Date and time on rules

Hello all

Tring to add 30 minute to datetime value
Works great on OH3 but on OH4 I’m getting error
The rule:

rule "test"
when
    Item test received command
then
    val timeRightNow = new DateTimeType(new DateTime((strBoiler_Start_Time.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli).plusMinutes(30).toString)
    logInfo("notifications", (timeRightNow.toString))
end

And this is the error:

2024-10-10 15:54:26.141 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'test-1' failed: An error occurred during the script execution: Cannot invoke "org.eclipse.xtext.common.types.JvmType.eIsProxy()" because "type" is null in test

Oded

strBoiler_Start_Time is DateTime item

DateTime strBoiler_Start_Time

This seems a little overdone. Why all this new this and new that and messing with eopch and such?

    val timeRightNow = (strBoiler_Start_Time.state as DateTimeType).zonedDateTime.plusMinutes(30).toString
    logInfo("notifications", timeRightNow)

That’s all you need.

As for the error, verify that the state of the Item isn’t NULL or UNDEF.

In the other rules langauges it’s even easier. For example in JS Scripting:

var timeRightNow = time.toZDT(items.strBooler_Start_Time).plusMinutes(30);
1 Like

It’s looks overdone because its came from rule that works fine on 3
I have to values on my sitemap
first when the switch triggered and the second 30 minute after that
I guess that my problem is casting datatype
This is my 2 item:

DateTime strBoiler_Start_Time
DateTime strBoiler_End_Time

And this is the rule that trow an error

rule "test"
when
    Item test received command
then

    strBoiler_Start_Time.postUpdate (new DateTimeType())
    strBoiler_End_Time.postUpdate = (strBoiler_Start_Time.state as DateTimeType).zonedDateTime.plusMinutes(30)
   
end

With var its work fine
What I’m missing?

Oded

It was overdone on OH 3 too. All that conversion stuff was unnecessary even back then.

postUpdate doesn’t block and wait for the Item to actually process the update and become the new state before returning. All that happens in the background and it takes some time. It’s almost never going to be the case, except on extremely slow machines or if the timing just happens to work just right, that strBoiler_Start_Time is going to be in the new state when you pull the state on the very next line of code.

You alreay know what you sent to the Item so just use that.

And postUpdate = (... makes no sense at all. postUpdate is a function, not a variable. You cannot assign something to a function to call it. Why not call it same as you did on the previous line?

    val startTime = now
    strBoiler_Start_Time.postUpdate(startTime)
    strBoiler_End_Time.postUpdate(startTime.plusMinutes(30))

Or if you are OK with the end time being ten or so microseconds more than 30 minutes in the future:

    strBoiler_Start_Time.postUpdate(now)
    strBoiler_End_Time.postUpdate(now.plusMinutes(30))

Are you using a chatbot to generate this code? It looks very much like the subtle nonsense ChatGPT or Gemini would suggest for OH rules. When it comes to OH, the chatbots are worse than useless because their code looks really close to correct but always contain some subtle thing wrong.

Try both and get this error

2024-10-10 19:03:11.526 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'test-1' failed: An error occurred during the script execution: Could not invoke method: org.openhab.core.model.script.actions.BusEvent.postUpdate(org.openhab.core.items.Item,org.openhab.core.types.State) on instance: null in test

I know that I’m not thinking like code guru but the code is mine

That’s odd. it used to work ok that way. It’s been a long time since I’ve done a whole lot in Rules DSL (it really is the worst of the options available now).

There are two things you can make this work, either one is as good as the other.

  1. postUpdate(startTime.toString)
  2. postUpdate(new DateTimeType(startTime))

postUpdate can always handle a String and DateTimeType is a State so post update can handle that too.

1 Like