more G-Labs products

Author Topic: Advanced Smart Lights  (Read 20534 times)

June 17, 2014, 07:54:59 AM
Read 20534 times

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
This is a large update to Gene's Smart Lights C# code.  This code will control a module with a motion sensor.  If the controlled module is a light (on/off light) or dimmer (dimmable light), it can also be optionally controlled by a wall switch transmitter.  If the module is a siren, it will treat it as a notification chime. 

Day/night is handled via jkUtils though Weather Underground is possible too with minimal modifications to the code.

There is an option for triggering the timer when motion is initially detected (on received) or after motion has stopped (off received).

The wall switch control has an advanced functionality beyond simply turning on and off the light module.  If a double on is received, it will turn the light on and enable the timer.  If a double off is received with the timer running, it will override and turn off the light immediately.  If a single off is received and the timer is on, it will not turn the light off.  In order to use this advanced switch control, it's also necessary to add the Last Event code.  This will allow HG to properly track duplicated on or off commands which is not possible by default.

The final code that is available is a stuck module check.  This is useful if you have a module that does not transmit properly every time or, more likely, the signal is not received consistently.  In this case, the stuck module check code will use a timer to turn the module off after a user specified timeout period.

I've spent a lot of time debugging, so hopefully everything works 100%.  If bugs are located, please let me know.  Enjoy!
« Last Edit: July 06, 2014, 07:10:38 AM by bkenobi »

June 19, 2014, 03:09:54 PM
Reply #1

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
I have seen a bug for some time that only crops up periodically in which the light controlled by the motion sensor will turn on and then immediately off.  I believe I have finally isolated the cause and fixed it.  If any bugs are found in this version, please let me know.
« Last Edit: July 06, 2014, 07:11:03 AM by bkenobi »

June 19, 2014, 04:18:52 PM
Reply #2

nolio

  • *****
  • Information
  • Global Moderator
  • Posts: 544
Hi,

I only try your "Last_Event-20140605.hgx" for now, because i have to install my light controller ;).
I like the idea to log the level of my equipments. That work fine and i want this for a long time.
I just add the function for "Shutter" and "DoorWindow".

Bye

June 19, 2014, 04:32:15 PM
Reply #3

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
I can't imagine working with this code without some kind of debug output.  I highly recommend using such a log in every code at least for the writing/debug phase!  I also have an Activity Log code that I can upload if anyone is interested.  I'll add it tonight (or when I remember).

June 19, 2014, 04:47:15 PM
Reply #4

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
I just remembered that I have a copy of the Activity Log code on my flash drive.  Enjoy!

Although the comment for this module indicate it outputs X10 commands, it is currently set up to output all HG activity.  If you want to just output a certain type of command, an if statement could be added to filter out any undesired entries.

WARNING:  This code will write a lot of lines to the specified file since HG performs a lot of checks for things like the Scheduler.  If you use the code as it is here, you will generate a large log file.  But, if you are running on a Raspberry Pi, this will increase the number of writes to your storage media significantly.  I have read that flash has a limited number of writes before it fails, so use of this code could decrease the life of your flash device.  Hypothetical?  Real?  Who knows, but you have been informed!   :D
« Last Edit: June 20, 2014, 06:11:24 PM by bkenobi »

June 20, 2014, 05:29:04 AM
Reply #5

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
I was thinking about the Activity log and realized that there were A LOT of activity events generated by the Scheduler and other HG modules.  These are probably not needed for most people, so I added a toggle for them in the Script settings.

June 24, 2014, 11:04:30 AM
Reply #6

nolio

  • *****
  • Information
  • Global Moderator
  • Posts: 544
Hi,

I use your "Last_Event-20140605.hgx" and write the log file on a share folder (usb key shared by my dd-wrt router). First to limit use of sd-card and then to have easily access for other use.

And with another rasp, i tweet every new line of the log file on a dedicated twitter account (and limited access account ;)).

It's just for fun and not very useful :).
But if some is interesting, i can describe the step and how exactly i did.

Bye

June 24, 2014, 02:24:37 PM
Reply #7

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
Very interesting.  Even if I don't use the method, I'd like to hear how you accomplished this.

June 24, 2014, 09:42:04 PM
Reply #8

nolio

  • *****
  • Information
  • Global Moderator
  • Posts: 544
So :
  • Mount my share folder
Test by mounting manually :
[/list]
Code: [Select]
mount -t cifs -o user=<xxx> //<@IP>/cle /mnt/cleWhen it work i had it on my fstab configuration to work on rasp next reboot :
Code: [Select]
vim.tiny /etc/fstabThen add a line like this :
Code: [Select]
//<@IP>/cle /mnt/cle cifs rw,user,username=<xxxx>,password=<xxxx> 0 0
  • Use bkenobi C# script
+ Import "Last_Event-20140605.hgx" in HG scenario.
+ Edit property of device you want to log, and then for "last event" check the checkbox.
+ Modify the path for the log by editing this scenario.
  • Install command line twitter
I use this web site :
http://xmodulo.com/2013/12/access-twitter-command-line-linux.html
But to get the pin code, i need to add this (because my rasp has no screen and can't launch a web browser) :
Code: [Select]
export LAUNCHY_DEBUG=true
t authorize -d
  • Create a script to tweet
I create a simple script to tweet each new line :
Code: [Select]
#!/bin/bash
tail -fn0 /mnt/cle/LastEvent.log | while read line
do
t update ?$line?
done

    June 25, 2014, 02:51:04 AM
    Reply #9

    bkenobi

    • *****
    • Information
    • Global Moderator
    • Posts: 1525
    Good tutorial.  Thanks for posting!

    Just curious if this was mostly to offload the twitter or if you think this will save writes in the long run?  The way I see it, this will shift the write from the main SD card on your Pi to a non-replaceable flash on your router.  I love the concept of writing to a different device to save writes on SD, but this seems to shift the writes to a less opportune media.  Thanks again!

    July 06, 2014, 07:10:16 AM
    Reply #10

    bkenobi

    • *****
    • Information
    • Global Moderator
    • Posts: 1525
    I updated all of the modules to fix a variety of issues that I found through further testing.  I recommend updating to this version since the older versions don't function quite right.

    Also, based on Gene's suggestions and nolio's guide, I have changed the logging default locations.  They are now located in the HG log path (/usr/local/bin/homegenie/log).  I mapped a path on my file server to the HG log path so that these files would be written to the other system rather than the Raspi SD card.  This is not required as the files will write to the drive as normal if nothing is mapped.

    Enjoy and let me know if you see any bugs.
    « Last Edit: July 06, 2014, 05:42:15 PM by bkenobi »

    July 06, 2014, 05:41:49 PM
    Reply #11

    bkenobi

    • *****
    • Information
    • Global Moderator
    • Posts: 1525
    I found one small mistake that is now fixed.  I couldn't test it last night because it was in the day code.  The issue relates to the chime implementation during the day and resulted in the nuisance check code not working correctly.  I think everything else is fine.

    July 13, 2014, 12:21:00 AM
    Reply #12

    dutchie

    • **
    • Information
    • Jr. Member
    • Posts: 47
    I found one small mistake that is now fixed.  I couldn't test it last night because it was in the day code.  The issue relates to the chime implementation during the day and resulted in the nuisance check code not working correctly.  I think everything else is fine.

    What should I do with the hgx file?
    Your smartlight probably is not what I'm looking for but it might contain something I could use. (My last programming experience was 30 years ago) and I've been across Gene's smartlight and the hue code but I cannot make my lights "softsart" with desired values and timers.  :'(

    August 04, 2014, 03:20:26 AM
    Reply #13

    bkenobi

    • *****
    • Information
    • Global Moderator
    • Posts: 1525
    I've tweaked a few small things over the last few weeks that seem to have made the system run perfectly.  Primarily, I was having issues with the IsOn and IsOff functions so I switched to using Parameter("Status.Level").DecimalValue instead.  I have not had any issues with the code since this change, so I'll share what I hope is the final release.


    dutchie:  Download the .hgx file to your administration machine (the system you connect to the HG web interface).  Go to HG -> Configure -> Automation.  I added a new group for my modules, but you can add to an existing one if you prefer.  Once you navigate to that group, select Actions -> Import Program.  Select the hgx file and confirm upload.  You will have to compile the program and then you will be able to configure your modules as you wish.

    August 04, 2014, 08:33:39 PM
    Reply #14

    dutchie

    • **
    • Information
    • Jr. Member
    • Posts: 47
    Thanks a lot bkenobi for the contribution (and your explanation to install it  ;)).
    After my vacation I'm gonna spend (a whole lot of) time to see if I can tweak it.
    I want the light to softstart (especially at night) and slowly increases to desired value and turns off after given time in the same way.