Problems with time comparison

Hi,

I am looking to switch from Smartthings to openHAB. I have zero Java experience and the last few days I am trying to adapt irrigation rule to suit my needs. The problem is that the rule was written in OH2 and the time comparison are modified in OH3 which I have running.
I did read several forum topics on the subject and I have to admit that all this Java stuff looks very scary and unfriendly for somebody who is just doing the home automation for hobby.
I am trying to execute some simple comparison:

  1. I have string item that has value in “HH:mm” format:

String itmIrrigationStartTime "Watering hour" <calendar>

  1. In the rule I have the following code:

     var DateTimeType userStartTime = parse(now.getYear() + "-" + now.getMonthValue() + "-" + now.getDayOfMonth() + "T" + itmIrrigationStartTime.state + ":00.000-01:00")
    
     //Check the time hour by hour minute by minute, whether we are checking the set time. If the system is already watering, it does not check the conditions and does not water.
     
         	if ( ( ( userStartTime.getHour() == now.getHour()  &&  userStartTime.getMinute() == now.getMinute() ) || checkUserStartTime == false )
     	&& itmIrrigationAuto.state == ON && (isWatering == false)
     ) {
    
  2. The rule execution reports the following error:

2020-12-27 16:20:00.180 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'irrigation-8' failed: 'getHour' is not a member of 'org.openhab.core.library.types.DateTimeType'; line 455, column 30, length 23 in irrigation

How to get this working?
I tried conversions of the hour and minute from the itmIrrigationStartTime to integer that I found on the following link, but it also didn’t work for me

Any help appreciated
Thanks

If you want to use it like you did in OH2 you need to replace:

now.getHour() with now.hour

But i would recommend to use something like now.isAfter() etc.

Thanks @spy0r, but it doesn’t work either. I don’t have problems with now.getHour(), that works (see logs below), but I have problem getting out the hour and minute from the user set time.
For testing I changed the code as follows and I still get error on “hour”.
Another option would be to extract the set hour and minute of the item itmIrrigationStartTime to integer, and then do the if comparison, but I couldn’t solve that also.

var DateTimeType userStartTime = parse(now.getYear() + "-" + now.getMonthValue() + "-" + now.getDayOfMonth() + "T" + itmIrrigationStartTime.state + ":00.000-01:00")

	logInfo( "FILE", " ======= Wheather algorithm  ======= ")
	logInfo( "FILE", "Irrigation_Main_algorithm: now                       [" + now + "]")
	logInfo( "FILE", "Irrigation_Main_algorithm: userStartTime             [" + userStartTime + "]")
	logInfo( "FILE", "Irrigation_Main_algorithm: checkUserStartTime        [" + checkUserStartTime + "]")
	logInfo( "FILE", "Irrigation_Main_algorithm: isWatering                [" + isWatering + "]")
	logInfo( "FILE", "Irrigation_Main_algorithm: itmIrrigationAuto.state             [" + itmIrrigationAuto.state + "]")

    //Check the time hour by hour minute by minute, whether we are checking the set time. If the system is already watering, it does not check the conditions and does not water.
	
	logInfo( "FILE", "Irrigation_Main_algorithm:   now.getHour()[" +  now.getHour() + "]")
	logInfo( "FILE", "Irrigation_Main_algorithm::  now.getMinute()[" + now.getMinute() + "]")

	if ( ( ( userStartTime.hour == now.getHour()  &&  userStartTime.minute == now.getMinute() ) || checkUserStartTime == false )
    	&& itmIrrigationAuto.state == ON && (isWatering == false)

The log:

2020-12-27 17:44:49.364 [INFO ] [org.openhab.core.model.script.FILE  ] - Irrigation_Main_algorithm: -START- 

2020-12-27 17:44:49.377 [INFO ] [org.openhab.core.model.script.FILE  ] -  ======= Wheather algorithm  ======= 

2020-12-27 17:44:49.383 [INFO ] [org.openhab.core.model.script.FILE  ] - Irrigation_Main_algorithm: now                       [2020-12-27T17:44:49.381786+01:00[Europe/Berlin]]

2020-12-27 17:44:49.387 [INFO ] [org.openhab.core.model.script.FILE  ] - Irrigation_Main_algorithm: userStartTime             [2020-12-27T01:00-01:00]

2020-12-27 17:44:49.393 [INFO ] [org.openhab.core.model.script.FILE  ] - Irrigation_Main_algorithm: checkUserStartTime        [null]

2020-12-27 17:44:49.398 [INFO ] [org.openhab.core.model.script.FILE  ] - Irrigation_Main_algorithm: isWatering                [null]

2020-12-27 17:44:49.403 [INFO ] [org.openhab.core.model.script.FILE  ] - Irrigation_Main_algorithm: itmIrrigationAuto.state             [ON]

2020-12-27 17:44:49.408 [INFO ] [org.openhab.core.model.script.FILE  ] - Irrigation_Main_algorithm:   now.getHour()[17]

2020-12-27 17:44:49.414 [INFO ] [org.openhab.core.model.script.FILE  ] - Irrigation_Main_algorithm::  now.getMinute()[44]

2020-12-27 17:44:49.419 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'irrigation-8' failed: 'hour' is not a member of 'org.openhab.core.library.types.DateTimeType'; line 464, column 11, length 18 in irrigation.

How would the syntax look like in the above if check?
Basically what I am trying to do is to trigger execution bunch of commands at the time specified by the user in the item itmIrrigationStartTime?

I think your userStartTime ist a different Type than “now”, its DateTimeType.

Openhab now uses Java Time API, but i’m not an expert in this and have to do lots of trial and error.

Did you read the release notes “rules” section?
See also this thread: DateTime Conversion (openHAB 3.x)

That was it, thanks @spy0r
"now"is Java time type so the correct conversion should be as described here :

val userStartTime = parse(now.getYear() + "-" + now.getMonthValue() + "-" + now.getDayOfMonth() + "T" + itmIrrigationStartTime.state + ":00.000+01:00").withZoneSameInstant(ZoneId.systemDefault())
1 Like