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
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
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.