HomeGenie Forum

General Category => General Discussion => Topic started by: Bobpick on November 25, 2014, 09:26:51 PM

Title: Randomization script
Post by: Bobpick on November 25, 2014, 09:26:51 PM
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!
Title: Re: Randomization script
Post by: mvdarend on November 26, 2014, 04:49:40 PM
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
Title: Re: Randomization script
Post by: Bobpick on November 26, 2014, 06:02:32 PM
It does help quite a bit! Thanks!!

As for the virtual switch, how was that accomplished?
Title: Re: Randomization script
Post by: mvdarend on November 26, 2014, 06:51:13 PM
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.)