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)
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.
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())
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.