more G-Labs products

Author Topic: How to randomize an event time?  (Read 3017 times)

May 15, 2015, 04:09:50 AM
Read 3017 times

JJ

  • *
  • Information
  • Newbie
  • Posts: 13
I am in the process of switching from AHP to HomeGenie. Currently I have two questions that I have not yet found answers for:
1 - Does some method exist to randomize an event time? In AHP I could specify "Security" and the event time would trigger +- 1/2 hour from the stated time. To cut that range a second event without security could be scheduled for the same action within the 1 hour period.  (Obviously not a true randomizatiion due to the range not included but ....)
2 - Have @Sunset / @Sunrise offsets been implemented yet?

Thanks,

May 15, 2015, 09:29:50 AM
Reply #1

nolio

  • *****
  • Information
  • Global Moderator
  • Posts: 544
For me, there is not this function yet in HG.


May 15, 2015, 10:53:03 AM
Reply #2

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
Here is an example I use when on vacation, this one is for the light in the attic. It's triggerd twice in the evening. (AwayFromHome is a virtual switch I created)

Trigger Code:
Code: [Select]
if (Scheduler.IsScheduling("15 19 * * 1-7") || Scheduler.IsScheduling("15 20 * * 1-7"))
{
  // Only run if AwayFromHome
  return Modules.WithName("AwayFromHome").Get().Parameter("Status.Level").Value == "1";
}
else
{
  return false;
}

Program to run:
Code: [Select]
Random rnd = new Random();
int myRandom = rnd.Next(1, 30); // creates a number between 1 and 30

Program.Notify("Attic", "Waiting " + myRandom.ToString() + " minutes");
Pause(myRandom * 60);

// Reading theme
Modules.WithName("Attic").Command("Control.ColorHsb").Set(".23393,.4763,.8");

myRandom = rnd.Next(1, 10); // creates a number between 1 and 10
Pause((myRandom + 5) * 60);
Program.Notify("Attic", "Waiting " + myRandom.ToString() + " + 5 minutes");

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

May 15, 2015, 05:17:39 PM
Reply #3

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
Nice example.  It would still be nice to have a code that was built in to do this though.  I think a toggle to enable a randomizer would be really nice.

May 16, 2015, 09:52:44 PM
Reply #4

JJ

  • *
  • Information
  • Newbie
  • Posts: 13
Can I add a request for an offset also?

Sunrise, Sunset, etc. are very useful but due to local conditions (trees, mountains) they sometimes don't accurately reflect the lighting environment.

May 16, 2015, 10:28:25 PM
Reply #5

dani

  • *****
  • Information
  • Global Moderator
  • Posts: 535
I agree With JJ for an offset.

May 17, 2015, 10:17:44 AM
Reply #6

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
One way to add an offset is to extend the functionality of the jkUtils - SolarAltitude program, just add the following code:

First add an extra function anywhere between existing functions:
Code: [Select]
SolarAltitude.addMinutes = function (date, minutes) {
    return new Date(date.getTime() + minutes*60000);
};


Then add the following four lines to the SolarAltitude.saveTimer block.
Code: [Select]
SolarAltitude.saveTimer("jkUtils.SolarAltitude.Morning.Sunrise.Start.Offset",SolarAltitude.addMinutes(sun.sunrise, -30),0);
SolarAltitude.saveTimer("jkUtils.SolarAltitude.Morning.Sunrise.End.Offset",SolarAltitude.addMinutes(sun.sunriseEnd, 30),-1);
SolarAltitude.saveTimer("jkUtils.SolarAltitude.Evening.Sunset.Start.Offset",SolarAltitude.addMinutes(sun.sunsetStart, -30),0);
SolarAltitude.saveTimer("jkUtils.SolarAltitude.Evening.Sunset.End.Offset",SolarAltitude.addMinutes(sun.sunset, 30),-1);

I've added 30 minutes to the Sunset.End and Sunrise.End values and subtracted 30 minutes from the Sunrise.Start and Sunset.Start values.

You can set this value to whatever you want, or even better, make it an Input Field
Code: [Select]
hg.Program.AddInputField("jkUtils.SolarAltitude.Offset", "30", "Offset in minutes");

May 17, 2015, 07:30:02 PM
Reply #7

JJ

  • *
  • Information
  • Newbie
  • Posts: 13
Thanks for the code. I am currently using the Weather Underground app but may need to look into switching.

But what I would really like is an offset that could be applied to any scheduler event so I could establish a base event for a certain time and stagger specific lights or switches from that event. eg - define a "PartyOver" event and turn on exit lights at -10 minutes, pool lights off at no offset, exterior lights off at +30 minutes, etc.

A potential syntax for the capability would be an optional 6th parameter on the cron string. Adding that and an optional 7th parameter for a randomizer +- time would supply both capabilities with no changes required to any existing configurations. Alternatively, the randomizer time could be just + time so the actual time could be implemented as a delay from the given time. (Of course any of this would require a wrapper or significant modification of the cron daemon if that is what is being used by HG.) Since the scheduler already allows combining events, the new parameters would not even need to be part of the base event.
« Last Edit: May 17, 2015, 09:34:29 PM by JJ »

May 18, 2015, 06:30:31 PM
Reply #8

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
There is a parser for cron event strings.  If additional arguments were included, the parser could potentially look for these and apply the needed offset.  I think you might be able to modify the Scheduled Events script to make this feature work, but I'm not sure since I have not looked.  If not, then the HG source would need updating thus requiring a feature request.

May 19, 2015, 11:25:47 PM
Reply #9

JJ

  • *
  • Information
  • Newbie
  • Posts: 13
I'm afraid that I don't know C# yet so I probably am not able to modify the Scheduled Events script until I learn it.

I have a script to modify an extended cron string (which includes offset and increment values) into a standard cron string. It is in Perl and the code is less than 100 lines. If anyone can translate that into C# I can send it.