Set criteria to time of day rather than

Hello all,

I have a rule, below, that I would like to replace the “Sunrise_End.state” item with a time of day i.e. Time cron “0 0 9 1/1 * ? *” (9:00AM). I’ve looked through the forums for a solution and I can’t find a similar rule to this. My initial thought is that I need to create an DateTime item for 9:00AM. However, I’m not sure how to accomplish this.

rule "Turn on Lights in the Morning"
	when 
		Item Multi_Living_Contact changed from CLOSED to OPEN
	then
    if(now.isBefore((Sunrise_End.state as DateTimeType).zonedDateTime.timeInMillis) &&
		Scene_General.state == 0)	{
			Morning?.members.forEach(Switch | {
				Switch.sendCommand(ON)
       		})
       }
end

9AM is not of DateTimeType as it’s not absolute (like “now” is) but a time of day to reoccur every day so you cannot represent it as a DateTimeType.
I don’t know your application but you’re likely better off using relative values such as now.getHourOfDay. There’s many ways, best is to read some of the threads on time conversion before proceeding. Watch out for implementations to use the astro binding which is likely to be a better basis for your application, too.

I settled on creating a new item MorningSwitch and a couple of rules to turn it on and off. It’s not very elegant, but effective. Thank you for taking your time to reply.

rule "Enable Lights in the Morning"
when   
		Time cron "0 0 6 1/1 * ? *"
then   
		sendCommand(MorningSwitch, ON)			
end

rule "Disable Lights in the Morning"
when   
		Time cron "0 0 9 1/1 * ? *"
then   
		sendCommand(MorningSwitch, OFF)			
end

rule "Turn on Lights in the Morning"
	when 
		Item Multi_Living_Contact changed from CLOSED to OPEN
	then
		if ((General_Night_State.state == ON) &&
		Scene_General.state == 0)	{
			Morning?.members.forEach(Switch | {
				Switch.sendCommand(ON)
       		})
       }
end

You might benefit from Design Pattern: Time Of Day.

Or to make things really easy if all you want to do is run a rule at a specific time of the day, you can use a cron statement in the when part of your rule