HomeGenie Forum

General Category => Troubleshooting and Support => Topic started by: roger.wills on November 03, 2015, 06:35:39 PM

Title: Alarm clock light automation
Post by: roger.wills on November 03, 2015, 06:35:39 PM
I have a Fibaro RGBW controller looking after a rgbw LED strip, all works as it should.

I want to link this strip to the alarm function on my android phone to turn on about 10 minutes before hand and gradually fade up to 100% luminosity.

I'm not aware of any capability built in that will allow me to do this out of the box, would be very happy to hear otherwise.

Breaking this down a bit. I intend to use Tasker on my Android phone to detect a new instance of an alarm whenever I set it (mainly because I already own that app) and then pass a trigger event to HG to run a script/programme to turn on the LED strip as described to a preset colour approximating daylight to help with waking up, I'll probably leave it on for a while before turning it off ready for the next alarm!

The Tasker end of things to detect a new alarm being set on the phone is straightforward enough for me to do, however, passing a trigger event to HG and then getting HG to gradually increase the level of the LED strip other than manually is beyond me at the moment. I've seen suggestions of event handlers on the forum but they were written for more technically minded people, programmers, not mere novices like me and are just out of my grasp of comprehension.

Can anyone help? I was quite surprised that this one didn't already exist somewhere.

Thanks.

Title: Re: Alarm clock light automation
Post by: roger.wills on November 04, 2015, 05:19:43 PM
Managed to look at this a bit more and used the Web API by launching urls from Tasker using the following format:

http://<hg_address>/api/<module_domain>/<module_address>/<command>/<param>

Looks like I can do all of this from Tasker itself but I think it would be tidier to run a programme from within HG at a specified amount of time before the alarm to gradually fade up the led strip.

So things I have to work out still are:

I am sure the latter is straightforward, but if anyone can point me in the right direction to do that I'd appreciate it.

Thanks
Title: Re: Alarm clock light automation
Post by: bkenobi on November 04, 2015, 10:50:43 PM
Code: [Select]
While (Program.IsEnabled
{
  module = Modules.WithName("LED_Strip");
  if (module.Parameter("Status.Level").Value < 100)
  {
    module.Parameter("Status.Level").Value = module.Parameter("Status.Level").Value +6;
    Pause(6);
  }
  else
  {
    return false;
  }
}

This is more of a pseudo code, but it should get you started.
Title: Re: Alarm clock light automation
Post by: roger.wills on November 07, 2015, 04:59:13 PM
thanks bkenobi.

Not sure what code this is, Python, C#, etc? I've tried pasting into HG as different code types but none work, there are errors!

I'm not really a programmer, I've dabbled in the past to do some specific simple things but that's it I really need to be held by the hand! To reference the specific Fibaro device I have I assume that I change the "Led_Strip" to something like "Bedroom_Alarm_Light_Strip" i.e. the specific name of the device on my system? I've already made this change to the code you provided on the assumption that is correct...



Title: Re: Alarm clock light automation
Post by: bkenobi on November 08, 2015, 01:59:44 AM
I use C# in HG.  The code I posted should be pretty close, but I didn't check it in HG to verify it worked so I expected it to have errors.  You should be able to past the code into an APP and put "Program.Run();" in the setup code (according to Gene, I haven't updated a script to use that approach yet).
Title: Re: Alarm clock light automation
Post by: mvdarend on November 08, 2015, 09:19:14 AM
Here is another way of doing it: (Code in C#)

Startup code:
Code: [Select]
return false; // Probably not needed anymore, still put it there out of habit :)
Program Code:
Code: [Select]
// Fibaro RGBW LED strip (HomeAutomation.ZWave)

int targetLevel = 50; // 100 is Maximum level
int timeInSeconds = 60; // 600 = 10 minutes

// Loop from 0 to targetLevel
for (int level = 0; level < targetLevel; level++)
{
  // 10.2 Red, 10.3 Green, 10.4 Blue, 10.5 White
  Modules.WithName("LED 10.2").Command("Control.Level").Set(level.ToString());
  Modules.WithName("LED 10.3").Command("Control.Level").Set(level.ToString());
  Modules.WithName("LED 10.4").Command("Control.Level").Set(level.ToString());
  Modules.WithName("LED 10.5").Command("Control.Level").Set(level.ToString());
  Pause(timeInSeconds / targetLevel);
}

In my case the LED strip is called LED strip Main and the color channels are 10.1, 10.2 etc. See the starup code of the FIbaro RGBW APP for more information on how it works.

I've set targetLevel to 50 and timeInSeconds to 60 to make it easier to test, change them to the desired values once it's working as you like.

I've also set it up so that all colors are increased equally, you can play around with it to get different Hues. You'll nee to experiment with that though, it the way the RGBW controller works sometimes the colours weren't changing as I'd expected.

You can run it from within the code editor, or from another Progarm with Program.Run("ProgramId"); or with a HTTP command, although I haven't tested that.
Title: Re: Alarm clock light automation
Post by: roger.wills on November 08, 2015, 10:15:14 AM
That's great thanks both, I'll try and let you know how I get on
Title: Re: Alarm clock light automation
Post by: roger.wills on November 08, 2015, 12:18:38 PM
Ok,

Now I have the syntax I'm getting this and have got a working program as below (can't seem to work out how to insert this as code like you have, the button doesn't seem to be working for me). Also note that I am using "Kitchen Floor Lights", an existing LED strip, to test this code and I will change that when the hardware arrives and I get the strip installed in the bedroom.

There is one issue that I can't seem to solve. When the LED strip first comes on the Level flares up to being almost 100% before it fades back down to level of 1, seems to work as it should thereafter. I assume there is something else I need to adjust somewhere so that we are not rudely awakened by this initial flare? (Note the second to last line of the Program code was an attempt to set the level low in case there was a memory effect when the strip was turned back on... clearly it had no effect!

Thanks.

Startup Code:
return false;

Program Code:
int targetLevel = 100; // 100 is Maximum level
int timeInSeconds = 600; // 600 = 10 minutes

Modules.WithName("Kitchen Floor Lights").Command("Control.ColorHsb").Set("224,213,177");
Modules.WithName("Kitchen Floor Lights").Command("Control.Level").Set("1");
// Loop from 0 to targetLevel
for (int level = 1; level < targetLevel+1; level++)
{
  Modules.WithName("Kitchen Floor Lights").Command("Control.Level").Set(level.ToString());
  Pause(timeInSeconds / targetLevel);
}
Pause(600);
Modules.WithName("Kitchen Floor Lights").Command("Control.Level").Set("1");
Modules.WithName("Kitchen Floor Lights").Command("Control.Level").Set("0");
Title: Re: Alarm clock light automation
Post by: mvdarend on November 08, 2015, 06:17:24 PM
It's probably this line:
Code: [Select]
Modules.WithName("Kitchen Floor Lights").Command("Control.ColorHsb").Set("224,213,177");
HSB = Hue, Saturation, Brightness, meaning that you're setting the Brightness to 177 (out of a possible 255), try setting it to 1. (You can probably lose the next line as well if this works.)
Title: Re: Alarm clock light automation
Post by: roger.wills on November 08, 2015, 06:45:56 PM
Thanks mvdarend.

Thought you had it when you suggested this, but alas, it made no difference. I recompiled, restarted HG service, but nothing changed with the suggested change!

Perhaps I need to revert to the method that you used originally to set each channel individually. I must confess to not understanding how to do the equivalent in my set up as I don't know what the individual channels are called here. How can I find out? Also, if your module is called "LED strip Main" how come you only refer to "LED 10.2" (etc) for each channel (i.e. you seem to have truncated the module name to the first 3 letters of the actual name?)? That confuses me and again in my set up I can't see what the equivalent would be?

Thanks for your help on this.
Title: Re: Alarm clock light automation
Post by: mvdarend on November 08, 2015, 06:58:45 PM
It's been a while since I last did much with the Fibaro RGBW module so I've forgotten exactly how they work.

I do remember that sometimes it didn't do exactly what I expected, so I ended up always controlling each colour channel separately in all my automation programs. (but to be honest, I can't remember exactly why I decided it was better to do that  :-[ )

Dani did document his findings quite well in the App itself, you can find the comments here:
Configure -> Programs -> Z-Wave -> Fibaro RGBW -> Edit -> Startup Code

Your LED channels most likely have the same addresses as mine Your LED channels will be 'sub-channels' of the main one (in your case the Addres for 'Kitchen Floor Lights'), you can find out in the 'Add Modules'  dialog (Configure -> Groups -> Choose a group -> Actions -> Add Modules), you should see something like the screenshot below.
Title: Re: Alarm clock light automation
Post by: mvdarend on November 08, 2015, 07:25:16 PM
Quote
HSB = Hue, Saturation, Brightness, meaning that you're setting the Brightness to 177 (out of a possible 255), try setting it to 1. (You can probably lose the next line as well if this works.)

What I said here is incorrect as far as the FibaroRGBW module goes...  :-[ I just had a look at the code and it doesn't work as I had expected.

HSB does mean Hue, Saturation, Brightness..., but the Fibaro module uses the values sent as RGB (Red, Green, Blue), so basically I was telling you to set Blue to 1...

That's probably why I ended up always controlling each colour channel separately, because the APP does that anyway.
Title: Re: Alarm clock light automation
Post by: roger.wills on November 08, 2015, 07:33:45 PM
OK, thanks will have another look.

Ref: ColorHSB, I thought that I was seeing RGB rather than HSB being changed, you've confirmed it and my sanity!
Title: Re: Alarm clock light automation
Post by: roger.wills on November 08, 2015, 08:28:53 PM
OK, I've tried applying the changes, again, it's not working  as expected.

However, I think I may have discovered a problem. When I checked the "module add" as per your screen shot I found as attached.

There is a multilevel switch and sub-channels only for Dimmer? Is that right? Looks like to me that something hasn't been updated when the Fibaro RGBW controller was added or it may be the way I've added it to the Group? I tried referencing these sub-channels in the code but they did not control the LED strip.

I'm going to look again at how to add the Fibaro RGBW controller in case I missed something, if anything seems obvious to you please shout!
Title: Re: Alarm clock light automation
Post by: roger.wills on November 08, 2015, 09:04:34 PM
Ok,

Finally I worked it out!

I didn't realise that I needed to add those Dimmer channels to the Group and also rename them. Now that I have done this and adjusted the code, all is working as required.

mvdarend, bkenobi, many thanks for being patient and talking me through it. The lack of non-techy oriented documentation makes these things difficult for folk like me! The learning curves are not just steep but there are many of them all running together, you never know as a beginner whether you are barking up the right tree to get things to work!

My only final question is, is there a way of making the "sub-channels" disappear in the mobile app (Android) so that it doesn't get cluttered? Note, I haven't upgraded to the new beta app yet, that was taking on too much at once!
Title: Re: Alarm clock light automation
Post by: roger.wills on November 08, 2015, 09:32:58 PM
Ok,

The code as it stands for anyone else wanting to follows below (again the "add code" button is not working for me for some reason so it's not as neat as it could be).

Explanation of the code...

It's written in C#.

targetlevel is the level of brightness that you want to raise the LED strip to, change it to whatever you want.

timeInSeconds - adjust this for the amount of time over which you want to raise the brightness of the strip, it's in seconds.

Each channel of the RGBW controller is raised in brightness equally (there is a separate channel for Red, Green, Blue and White, i.e. 4 in all). In this case it is incremented by 1% of brightness every 6 seconds. The code calculates how long a pause to put between each 1% raise of brightness for you.

Finally, once at 10% brightness (targetlevel is set at 10) the code pauses for 6 seconds and then turns the LED strip off. As this is for an alarm function this allows for some time to get out of bed and switch other lights on before it switches off automatically so you don't have to remember to do that when you are half asleep, it's in seconds again so change that for however long you want, I'm going to set it at 600 seconds, i.e. 10 minutes.

Don't forget to add the sub-channel modules to the group (as well at the main FibaroRGBW module) otherwise the code doesn't work! In my case the Fibaro Controller was module 7 and the sub-channels were modules 7.1, 7.2, 7.3, 7.4 and 7.5.

Note to eliminate all confusion for beginners like me, when I added these sub-channel modules I renamed them from "Dimmer 7.2", "Dimmer 7.3", etc to "Kitchen Floor 7.2", "Kitchen Floor 7.3", etc.

Note I was using an existing Fibaro RGBW controller and strip already installed into my kitchen to work the code out for this. Simply change all instances of Kitchen Floor or Kitchen Floor Lights to whatever you called your modules.

Startup Code:
return false;

Program Code:
int targetLevel = 10; // 100 is Maximum level
int timeInSeconds = 60; // 600 = 10 minutes

// Loop from 0 to targetLevel
for (int level = 0; level < targetLevel; level++)
{
  Modules.WithName("Kitchen Floor 7.2").Command("Control.Level").Set(level.ToString());
  Modules.WithName("Kitchen Floor 7.3").Command("Control.Level").Set(level.ToString());
  Modules.WithName("Kitchen Floor 7.4").Command("Control.Level").Set(level.ToString());
  Modules.WithName("Kitchen Floor 7.5").Command("Control.Level").Set(level.ToString());
  Pause(timeInSeconds / targetLevel);
}
Pause(6);
Modules.WithName("Kitchen Floor Lights").Command("Control.Level").Set("0");
Title: Re: Alarm clock light automation
Post by: mvdarend on November 09, 2015, 09:26:05 AM
Great that you got it working! It was approx. 18 months ago that I set up the Fibaro RGBW module, and I've forgotten a lot of the problems I ran into, it looks like you've gone down the same track :)

I'd completely forgotten that I had to name the sub-channels, for some reason I gave them a very generic name, so I assumed yesterday that they were already named that by the plugin.

Quote
My only final question is, is there a way of making the "sub-channels" disappear in the mobile app (Android) so that it doesn't get cluttered? Note, I haven't upgraded to the new beta app yet, that was taking on too much at once!
Now that you've named the sub-channels you should be able to remove them from the group, they will keep the names you have given them. (I no longer have mine in groups and all is working fine.)
Title: Re: Alarm clock light automation
Post by: roger.wills on November 09, 2015, 10:32:25 AM
Thanks again, no way I could have worked it out without you more or less handing it to me on a plate!

I can confirm that removing the sub-channel modules from the group has worked.
Title: Re: Alarm clock light automation
Post by: mvdarend on November 18, 2015, 10:36:31 AM
Could you show us how you set up Tasker to read your alarm settings? (Are you using Auto-Alarm or some other plugin?)
Title: Re: Alarm clock light automation
Post by: roger.wills on November 18, 2015, 12:14:05 PM
Hi Marcel,

I am indeed using the Autoalarm plugin.

I have set up a Profile that runs a task at 2.00 am every morning as I have usually set any alarms I need by then, the nights when I am still up at that time usually mean I am planning on writing off the next day!!!

So the task contains the following actions:

1. AutoAlarm - the plugin action, needs no configuration, just select it from the plugins. It runs when you run the task and returns various information about the next upcoming alarm.
2. If - Set up an If action that checks one of AutoAlarms variables, in my case I used the total number of minutes (%minutes) until the next alarm, if that is equal to 0 then I stop the task with a Stop command which you can find in the Task category. I found that there seemed to be no other way to stop tasker from looping if I didn't put this in. What it does is to stop the task from doing anything if there is no alarm... you don't want the light coming on randomly in the middle of the night after all!
3. Stop - this is the stop action already mentioned in "2", above.
4. End If - no configuration needed, just lets Tasker know that the If statement has finished.
5. If - So to the meat of the Task. This If statement checks if the %minutes variable is not equal to 0. Assuming that it does not, in other words that there is an alarm set, then it runs the next two actions...
6. Wait - the task is set to wait until 10 minutes before the alarm by simply setting the Minutes variable in the Wait action to %minutes - 10. Note if you change the amount of time in the HG program over which you want to raise the brightness of the LED strip, then you need to adjust this as well otherwise it will be out of step and you'll get confusing activity.
7. HTTP Post - You use this action (from the Net category) to make a Web API call which runs the program posted above. I've posted about how use Web API elsewhere to turn a driveway light on and off which I'll post a link to shortly for a fuller explanation, but the format of the call is http://<HG Server IP Address:Port>/api/HomeAutomation.HomeGenie/Automation/Programs.Run/1006 . Change the program number to the number of your program in HG and change the <HG Server IP Address:Port> to the IP address of your HG server whatever that is and the port number.
8. End If - ends the If statement.

That's it. Hopefully that's enough to allow you to replicate and adjust for your own purposes.
Title: Re: Alarm clock light automation
Post by: roger.wills on November 18, 2015, 12:18:12 PM
Ok, the other topic was called "Controlling Modules from Outside Home Network" and can be found here: http://www.homegenie.it/forum/index.php?topic=1189.0 (http://www.homegenie.it/forum/index.php?topic=1189.0)

I will probably add the sunset check to the program code for the Alarm Clock as well to make it a bit more energy efficient but obviously get it to check for sunrise rather than sunset!
Title: Re: Alarm clock light automation
Post by: mvdarend on November 18, 2015, 03:46:33 PM
Thanks for that Roger, I'll give it a try this evening.
Title: Re: Alarm clock light automation
Post by: roger.wills on November 24, 2015, 07:46:02 PM
Marcel,

I hope you can help. I need to find a way to interrupt the alarm program to turn off the LED strip in case of accidental running, say I don't turn off an alarm the night before and my lie in on Sunday morning is rudely interrupted! At the moment if I manually turn off the strip, the program is still running and as soon as it loops again it turns them back on and continues to the end of the program.

I need to be able to interrupt this and turn the lights off and keep them off until I set another alarm of course.

I've tried a couple of ways but had not success, both modifying the C# code to check each time it loops to see if the LED strip has been turned off and a separate wizard script triggered every time the LED strip is turned off (Status.Level <1). Neither worked. I am clearly doing something wrong and Google has come to the end of suggestions.

Cheers.

Title: Re: Alarm clock light automation
Post by: bkenobi on November 24, 2015, 07:58:52 PM
If you turn the LED strip off locally then HG won't know they are off unless your switch sends a status (X10 don't but I think most others such as zwave do).  If HG knows that the strip has been turned off, simply check the "status.level" parameter after the first iteration and if it's zero, break.
Title: Re: Alarm clock light automation
Post by: mvdarend on November 24, 2015, 08:20:03 PM
I've tried a couple of ways but had not success, both modifying the C# code to check each time it loops to see if the LED strip has been turned off and a separate wizard script triggered every time the LED strip is turned off (Status.Level <1). Neither worked.
Are you turning the LEDs off through HomeGenie? then I would expect that checking the status level should work. You might need to get the decimal value, something like this:

Code: [Select]
// Loop from 0 to targetLevel
for (int level = 0; level < targetLevel; level++)
{
  if (Modules.WithName("Kitchen Floor 7.2").Get().Parameter("Status.Level").DecimalValue == 0)
  {
    break;
  }

  Modules.WithName("Kitchen Floor 7.2").Command("Control.Level").Set(level.ToString());
  Modules.WithName("Kitchen Floor 7.3").Command("Control.Level").Set(level.ToString());
  Modules.WithName("Kitchen Floor 7.4").Command("Control.Level").Set(level.ToString());
  Modules.WithName("Kitchen Floor 7.5").Command("Control.Level").Set(level.ToString());
  Pause(timeInSeconds / targetLevel);
}

As Bkenobi has said, if you're switching off the lights manually, HomeGenie might not (yet) know their status. Do you have level polling switched on? I'm not sure if that also gets the light values.

Another option if you can't get anything like this to work is to create a Virtual switch, say for instance a switch called ''Disable WakeUp'

And then exit the loop if it has been switched on, like this:

Code: [Select]
if (Modules.WithName("Disable WakeUp").Get().Parameter("Status.Level").Value == "1")
{
  break;
}

You could also use the 'Automatic turn off' feature to flip the virtual switch back to 'Off' after a few minutes.

Edit - I checked the level polling and the standard settings are to check once every 10 minutes, so that won't help you much in this case. You can lower it, but I have no idea of the performance impact if you set it to every few seconds.
Title: Re: Alarm clock light automation
Post by: swaner on November 30, 2015, 01:56:35 PM
I hope you can help. I need to find a way to interrupt the alarm program to turn off the LED strip in case of accidental running, say I don't turn off an alarm the night before and my lie in on Sunday morning is rudely interrupted! At the moment if I manually turn off the strip, the program is still running and as soon as it loops again it turns them back on and continues to the end of the program.

Hi Roger, what I did was using tasker and another http request when I wanted to "cancel" the alarm (program) in homegenie. Send the restart command to HomeGenie and the code will stop executing:
http://<HG Server IP Address:Port>/api/HomeAutomation.HomeGenie/Automation/Programs.Restart/1006.
Title: Re: Alarm clock light automation
Post by: roger.wills on November 30, 2015, 04:15:41 PM
Thanks Swaner, I'm trying to do everything from Homegenie where I can, I naturally go for the HG app every time to turn off the LED strip so need to solve within the C# script if I can. Your approach is at least a fallback position.

Marcel, I've implemented the line of code you suggested although had to use the "parent" module for want of a better label, i.e. "Kitchen Floor" as that is what colour wheel that I have added actually controls rather that one of the sub-channels.

It works to a degree in that the strip is turned off and stays off, but I have discovered that the script is still actually running. Although I have desired effect i.e the LED strip is off and stays off, if I try running the script again, it won't restart and run again.

I think I need the "restart" command as per Swaner's approach but within the C# script and I can't work out what it is. Everything I have tried is not recognised... got any ideas?
Title: Re: Alarm clock light automation
Post by: mvdarend on December 09, 2015, 11:07:29 AM
Sorry, I only just noticed your last post.

To be honest, I'm not sure why it would keep running. I would have expected it to just stop as it would if you didn't break out of the loop.

Maybe a Program.GoBackground(); at the end? (just a guess here, have no idea if it will help)