DSL rule behaves unexpected

  • Platform information:
    • Hardware: Intel
    • OS: _Windows10
    • Java Runtime Environment: OpenJDK 64-Bit Server VM Zulu11.41+23-CA (build 11.0.8+10-LTS, mixed mode)
    • openHAB version: 3.3 snapshot
  • Issue of the topic: Script execution of rule with UID ‘Gertshuis-19’ failed: Unknown variable or command ‘-’; line 334, column 29, length 38 in Gertshuis
  • Please post configurations (if applicable):
    • Items configuration related to the issue
      Number Flowmeter “Flowmeter [%.3f m³]” (Status)
      Number Flowmeterstart “Flowmeterstart [%.3f m³]” (Status)

    • Sitemap configuration related to the issue: none

    • Rules code related to the issue

Flowmeter.postUpdate(447.177)
Flowmeterstart.postUpdate(447.170)
logInfo(Test1, 90 Flowmeter + Flowmeter.state.toString() + , Flowmeterstart + Flowmeterstart.state.toString())
logInfo(Test1, 91 + (447.177-447.170).toString())
logInfo(Test1, 92 + (Flowmeter.state - Flowmeterstart.state).toString())

  • Services configuration related to the issue
  • If logs where generated please post these here using code fences:

2022-03-10 22:03:00.143 [INFO ] [org.openhab.core.model.script.Test1 ] - 90 Flowmeter 447.177, Flowmeterstart 447.17
2022-03-10 22:03:00.143 [INFO ] [org.openhab.core.model.script.Test1 ] - 91 0.007000000000005002
2022-03-10 22:03:00.144 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘Gertshuis-19’ failed: Unknown variable or command ‘-’; line 334, column 29, length 38 in Gertshuis

What is wrong ? Why does the line with Test1, 92 result in ERROR whwn I expect the same as in Test1, 91

I think because state objects do not have a subtract operation. You would have to cast it/them as a number first.

But that will not happen anyway. Items are not variables, when you postUpdate() a new value it is not applied immediately and your rule does not stop and wait for it to happen.

logInfo(Test1, "92 " + ((Flowmeter.state as DecimalType).intValue - (Flowmeterstart.state as DecimalType).intValue).toString())

Rules DSL is quite cumbersome.

In jruby, this would be:

logger.info("92 #{Flowmeter - Flowmeterstart}")

With jruby you can “treat” them like numbers and perform arithmetic directly. It would even deal with unit conversions automatically if the items have dimensions.

However, as @rossko57 said, Items aren’t the same as variables. When you send an update to the item, it will not update immediately. The “update” is posted to the bus event, then another process will process through the messages on the queue and then apply the state to the item + execute any triggers on the system related to the item.

If you want to do this, you should use an actual variable and perform the calculations on the variables as well as post that to the item.

1 Like

Thanks for your reactions. I changed the code to:

Flowmeter.postUpdate(447.177)
Flowmeterstart.postUpdate(447.170)
var Number A = Flowmeter.state
var Number B = Flowmeterstart.state
var Number W = A - B
logInfo(Test1,W = + W.toString())

Now it works.

You don’t seem to have understood the comments about postUpdate() working asynchronous.

Indeed I agree with this observation.

Flowmeter.postUpdate(447.177)
Flowmeterstart.postUpdate(447.170)
var Number A = Flowmeter.state // IN HERE, Flowmeter.state may not have been updated yet
var Number B = Flowmeterstart.state // SAME HERE
var Number W = A - B
logInfo(Test1,W = + W.toString())

Instead, your code should look like this:

var Number A = 447.177
var Number B = 447.170
var Number W = A - B
Flowmeter.postUpdate(A)
Flowmeterstart.postUpdate(B)
logInfo(Test1,W = + W.toString())

Also in the future, you should enclose your code with triple backticks when posting on the forum. They are called “code fence”. See: Extended Syntax | Markdown Guide

Next step: you should consider using Units of Measurement for your items whenever applicable.

This means that your items should look like this:

Number:Volume Flowmeter "Flowmeter [%.3f m³]" (Status)
Number:Volume Flowmeterstart "Flowmeterstart [%.3f m³]" (Status)

But this is a whole different rabbit hole to explore.

1 Like