Design Pattern: Simple State Machine (e.g. Time Of Day)

I am trying to add Hours to a certain time already set when the lights were turned on.

Once I turn on the light, it saves the time. Then according to selected time in sitemap it takes that and add (x) Hours to the total time. and then checkes every 15 minutes until the .isAfter time is active then shuts them off automatically. I am unsure how to compare the times.


rule "Grow Lights auto off" 
when
	Time cron "0 */15 * ? * *"
then
	 //Grab the selected hours from sitemap
    var Integer GrowLightsTime=(GrowLights_Time.state)
	// Epoch time to compare start time with hours selected by sitemap
    val Long StartTime = (GrowLights_StartTime.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli
	
	

    //This is where i am unsure how to compare the start time to the end time to create a condition and always gives gets errors I know the line below does not work, just giving you an idea
    // what i'm trying to acomplish
	val Long end_time = (new DateTime((GrowLights_StartTime.state as DateTimeType).zonedDateTime.toInstant().toEpochMilli).plusHours(GrowLightsTime))
	
    //This is also where i am also unsure how to compare to  and would like to do it
	if (now.isAfter(end_time)){
		if (GrowLights_Power.state!=OFF){
			GrowLights_Power.sendCommand(OFF) 
		}
	}


end

Thanks for any help guys !

Why so complicated?
Just start a timer when you switch on the light and use your “sitemap” item’s state as the duration.
When the timer has run out, switch off the lights. When you manually switch them off and on during that timeframe, cancel and re-start the timer.

var Timer t = null

rule "Lights"
when
        Item light_item changed to ON
then
        if (t !== null) t.cancel        // cancel timer if started
                        t = createTimer(now.plusHours(duration_item.state), [ |        // start timer
                        light_item.sendCommand(OFF)    // when elapsed send OFF
                        t = null                            // reinitialize Timer
                        ])

end

(not tested, but you should get the idea)

that is my current running situation. From sunrise start a timer and then shut off after the time. This is more if i change the hours while it is running it will then compare the times and turn on and off instead of add that extra hours on top of when it has been running… if that makes sense.

Umm, no, I currently don’t know what behavior you want exactly…

Start a new thread with this. It’s completely unrelated to the topic here.

1 Like

Whoops, I though I was posting in DateTime Conversion (openHAB 3.x)

Sorry, I was researching so much I must have posted in the wrong topic. Sorry.

For what its worth, I do think that’s an interesting topic worthy of its own thread.

1 Like