Putting Together DateTime on Variable

I’m getting this error using some code from @rlkoshak I found on the forum.

2024-01-30 19:40:56.149 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'home-3' failed: An error occurred during the script execution: Could not invoke method: java.time.ZonedDateTime.withHour(int) on instance: 2024-01-30T19:40:56.147866581+01:00[Europe/Berlin] in home

This syntax works fine when set the hour and minute as a number:

MQTTDishWasherDeadline.postUpdate(now.withHour(5).withMinute(0).withSecond(0).withNano(0).toLocalDateTime.toString())

If you try to add any variable into the mix then I get the error above. It can be a item variable or a on the fly variable, same result. Tried Number and String assignment for the variable, No go. Tried ‘as Number’ and .toString(), No go.

MQTTDishWasherDeadline.postUpdate(now.withHour(DummyHour).withMinute(DummyMinute).withSecond(0).withNano(0).toLocalDateTime.toString())

Any ideas?

Best, Jat

This is Rules DSL?

You are working with a raw Java Class. So you don’t get any “magic” type conversion done for you. .withHour(), per the [docs](ZonedDateTime (Java SE 17 & JDK 17) requires a primitive int. It can’t be a Number, it can’t be a String or a DecimalType or a BigDecimal. It must be an int.

But Rules DSL wants everything to be a BigDecimal and will convert stuff to become a BigDecimal for you whether you want it to be or not.

  1. Number variable: now.withHour(DummyHour.intValue)
  2. State of an Item: now.withHour((MyItem.state as Number).intValue)
  3. Result of a calculation involving a variable: now.withHour((DummyHour + 7).intValue)

It’s stuff like this which, once Blockly became viable, has caused me to no longer recommend Rules DSL for any new development. I also recommend when you need to update a rule, consider migrating it to Blockly or one of the other languages. The advantages Rules DSL once had have long since been surpassed and Rules DSL is now a quite inferior rules language.

In Blockly the line above would look like:

The blue “5” there shows the block you would use to insert the variable or a calculation instead of hard coding the number.

In JS Scripting it would look like this:

items.MQTTDishWasherDeadline.postUpdate(time.toZDT('05:00'));

jRuby will have an equally concise line of code for this.

Thank you! Working on a clients new OH installation overseas remotely and this was a requirement I could NOT figure out for the life of me.

Best, Jay