more G-Labs products

Author Topic: Alarm clock light automation  (Read 4653 times)

November 08, 2015, 09:32:58 PM
Reply #15

roger.wills

  • ***
  • Information
  • Full Member
  • Posts: 53
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");

November 09, 2015, 09:26:05 AM
Reply #16

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
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.)

November 09, 2015, 10:32:25 AM
Reply #17

roger.wills

  • ***
  • Information
  • Full Member
  • Posts: 53
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.

November 18, 2015, 10:36:31 AM
Reply #18

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
Could you show us how you set up Tasker to read your alarm settings? (Are you using Auto-Alarm or some other plugin?)

November 18, 2015, 12:14:05 PM
Reply #19

roger.wills

  • ***
  • Information
  • Full Member
  • Posts: 53
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.

November 18, 2015, 12:18:12 PM
Reply #20

roger.wills

  • ***
  • Information
  • Full Member
  • Posts: 53
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

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!

November 18, 2015, 03:46:33 PM
Reply #21

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
Thanks for that Roger, I'll give it a try this evening.

November 24, 2015, 07:46:02 PM
Reply #22

roger.wills

  • ***
  • Information
  • Full Member
  • Posts: 53
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.


November 24, 2015, 07:58:52 PM
Reply #23

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
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.

November 24, 2015, 08:20:03 PM
Reply #24

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
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.
« Last Edit: November 25, 2015, 07:22:24 AM by mvdarend »

November 30, 2015, 01:56:35 PM
Reply #25

swaner

  • *
  • Information
  • Newbie
  • Posts: 21
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.

November 30, 2015, 04:15:41 PM
Reply #26

roger.wills

  • ***
  • Information
  • Full Member
  • Posts: 53
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?

December 09, 2015, 11:07:29 AM
Reply #27

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
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)