Hello,
I have a var lightOn of the DateTime format in my .rules file at which I want to triller the rule.
Unfortunately I’m unable to find a solution to do this. My approach with
when
Time is lightOn
is not working.
I would be very happy about helpful suggestions on how to solve this problem.
John
You’ll want to look at various “alarm clock” examples for the techniques to carry out this task - “do something” at a time set by an Item state.
You cannot directly do what you are trying - run a rule at a variable time.
But you can run a rule when your Item changes (or once each day etc.) and have that rule set up a future event at the given time.
rule "Turn External Lights off from 23:30"
when
Time cron "0 30 23 ? * * *"
then
gExternalLights.members.forEach[ temp |
var int randomTime = (new java.util.Random).nextInt(1800)
createTimer(now.plusSeconds(randomTime)) [|
temp.sendCommand(OFF)
]
]
end
I use this so all lights do not go off at the same time every night so it never appears that I am using automation and not home.
As indicated by the other posts, you can’t trigger a Rule like that. However, also as shown, you can use Timers and a Rule that triggers at a certain time every night to recreate the Timer based on the value of lightOn.
I have a couple of reusable scripts in Jython that would do this for you. First there is item_init which lets you ad metadata to an Item to initialize it at startup. So you could create a LightOn Item DateTime Item and initialize it to an arbitrary time.
Next you could use the ephem_tod either as is or use it as inspiration to create something simpler. This similarly lets you set Item metadata on that same LightOn Item and it will sendCommand to a TimeOfDay Item. You can then use that to trigger your rule.