Rule compare dayTime as trigger

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.

Adding to what rossko57 mentioned you can also have a rule turn on/off something at a random time.

Example for randomly turning lights off at a set time:
Items:

Group gExternalLights "External Lights" <light> 

Switch	Light_Porch	"Porch Lights [%s]" <light> (gExternalLights)
Switch	Light_Patio	"Patio Lights [%s]" <light> (gExternalLights)
Switch	Light_Deck	"Deck Lights [%s]" <light> (gExternalLights)

Rule:

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.

What if you’re still awake (and outside)…? :smiley:

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.

Once both are installed all you need to do is:

DateTime LightOn { init="16:15:00", etod="LIGHT-ON"[type="default"]

And make your rule:

when
    Item TimeOfDay received command LIGHT-ON

No coding required. But it does require setting up Jython with the Helper Libraries and copying the above to your config.

1 Like

Thank you all for your answers. I used a timer that triggers the needed action at lightOn.