How to add Hours/Minutes to pre-determined time

I am running OpenHAB3. 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 duration in sitemap it takes that and add (x) Hours to the total time. Then checks 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 guys,

I am a big fan of using expire especially if you are going to leave the lights on for a certain time each time the lights are turned on.

First of all this is an XY Problem. Your proposed approach is not the best approach to solve this problem.

If you always want to shut off the light X hours where X is a hard coded value:

  • Navigate to the light Item
  • click on “add metadata” and select “Expire” from the list
  • configure “Expire” to command the Item to OFF after the desired number of hours.

If you want to be able to adjust the hours via an Item create a rule and use a Timer.

var Timer timer = null

rule "Grow Lights auto off"
when
    Item GrowLights changed to ON
then
    // Cancel the timer if it already exists
    timer?.cancel
    timer = createTimer(now.plusHours((GrowLightsTime.state as Number).intValue), [ |
        GrowLights.sendCommand(OFF)
    ]
end

In just three lines of code (not counting the rule setup) not only does this do what you are after, it does so in fewer lines of code, requires fewer Items (you don’t have to keep track of the start time) and the light will turn off at exactly X hours, not X hours plus up to 15 minutes.

Now, in reference to your attempted solution, I see the following issues:

  • A Number Item’s state is not an Integer. It is a DecimalType. A DecimalType is itself of type Number but a Number is not necessarily an Integer. So you need to call intValue to get the Item’s state as an integer.

  • Don’t mess with epoch. There is no reason to and the code is much easier to read if you don’t. ZonedDateTime has almost everything you need to manipulate times and what it lacks (e.g. subtracting one time from another, Duration provides.

  • There is no such thing as DateTime in OH 3. You can’t create a new one. It has been replaced with ZonedDateTime instead.

  • If I recall correctly, even Joda DateTime (what we used before OH 3.0) you had to pass isAfter and isBefore a DateTime Object, not epoch. Again, don’t mess with epoch.

So if I were to correct the errors of the above code it would be

	 //Grab the selected hours from sitemap
    var GrowLightsTime=(GrowLights_Time.state as Number).intValue
	// Time to compare start time with hours selected by sitemap
    val startTime = (GrowLights_StartTime.state as DateTimeType).zonedDateTime
	val endTime = startTime.plusHours(GrowLightsTime)
	
    //This is also where i am also unsure how to compare to  and would like to do it
	if (now.isAfter(endTime)){
		if (GrowLights_Power.state!=OFF){
			GrowLights_Power.sendCommand(OFF) 
		}
	}

Isn’t that much more straight forward? Far fewer conversion from different date time types to epoch and back again.

I get what your saying… I am just trying to get as much information as possible for WHEN I run into some more trouble that I might need help on ha ha. I am still fairly new to coding so I do a lot of research when I run into errors or try to implement something new, especially coming from OH2 to OH3. There was a lot of syntax in the rules changes I had to amend. This is a simple yet effective way to complete what I am looking for. Can always count on you to help out :+1: Thanks again.