more G-Labs products

Author Topic: Randomization script  (Read 1481 times)

November 25, 2014, 09:26:51 PM
Read 1481 times

Bobpick

  • *
  • Information
  • Newbie
  • Posts: 22
My wife and I live alone.

I want the home to look more lived in by HG randomly turning on assigned lights, even at 3 in the morning.

What would that script look like?

Thanks!

November 26, 2014, 04:49:40 PM
Reply #1

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
Under Configuration -> Automation -> Security you can find the Presence simulator module, but I found that this didn't meet my needs. I wanted something more like what you have described.

I've done the following:

Created a virtual Switch named 'AwayFromHome', I can switch this on and off from the App or with a small remote control. (You don't need this though, you can just enable/disable the Programs by hand)

I then created a number of different scripts, here is an example for the bathroom lights:

Trigger code:
Code: [Select]
// Set some trigger times

if (Scheduler.IsScheduling("15 19 * * 1-7") ||
    Scheduler.IsScheduling("30 21 * * 1-7") ||
    Scheduler.IsScheduling("00 7 * * 1-7") ||
    Scheduler.IsScheduling("10 01 * * 1-7") ||
    Scheduler.IsScheduling("20 04 * * 1-7"))
{
  // Only run if 'AwayFromHome''
  return Modules.WithName("AwayFromHome").Get().Parameter("Status.Level").Value == "1";
}
else
{
  return false;
}

Program code:
Code: [Select]
// First a random pause
Random rnd = new Random();
int myRandom = rnd.Next(1, 26); // creates a number between 1 and 25

Program.Notify("Bathroom", "Waiting " + myRandom.ToString() + " minutes");

Pause(myRandom * 60);

Modules.WithName("BathroomLight").On();

if (DateTime.Now.Hour < 6)
{
  // Late at night, Lights on for a few minutes
  myRandom = rnd.Next(1, 5); // creates a number between 1 and 4
}
else
{
  myRandom = rnd.Next(1, 21) + 10; // creates a number between 10 and 30
}

Pause(myRandom * 60);

Modules.WithName("BathroomLight").Off();

Hope this helps

November 26, 2014, 06:02:32 PM
Reply #2

Bobpick

  • *
  • Information
  • Newbie
  • Posts: 22
It does help quite a bit! Thanks!!

As for the virtual switch, how was that accomplished?

November 26, 2014, 06:51:13 PM
Reply #3

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
As for the virtual switch, how was that accomplished?

Attached is the APP code for the virtual switches. I'm currently using only two, (AwayFromHome and WakeUpLight) but you can add more by editing this line in the trigger code:

Code: [Select]
Program.AddVirtualModules("HomeAutomation.SettingSwitch", "Switch", "homegenie/generic/switch", 1, 2);
(Change the '2' at the end to the number of virtual switches you want created.)