danst
(Daniel)
January 28, 2022, 7:57am
1
Hi,
I try to add a number to a number Item, however w/o success. What am I doing wrong?
Daniel
rule:
logInfo("bewegungsmelder.rules", "EG_AZ Counter hoch: " +(EG_AZ_PRAESENZ_Counter.state as DecimalType))
if (EG_AZ_PRAESENZ_Counter.state < 15) {
logInfo("bewegungsmelder.rules", "EG_AZ Counter hoch: " +(EG_AZ_PRAESENZ_Counter.state as DecimalType))
EG_AZ_PRAESENZ_Counter.sendCommand((EG_AZ_PRAESENZ_Counter.state as DecimalType) + 3.0)
logInfo("bewegungsmelder.rules", "EG_AZ Counter hoch: " +(EG_AZ_PRAESENZ_Counter.state as DecimalType))
}
Log Output:
2022-01-28 08:56:25.917 [INFO ] [e.model.script.bewegungsmelder.rules] - EG_AZ Counter hoch: 1.40
2022-01-28 08:56:25.918 [INFO ] [e.model.script.bewegungsmelder.rules] - EG_AZ Counter hoch: 1.40
2022-01-28 08:56:25.919 [INFO ] [e.model.script.bewegungsmelder.rules] - EG_AZ Counter hoch: 1.40
You need to do the calculation in your if differently I think. Example:
var item = youritem.state
var item = item + 1;
I am sure you can also do it in one line somehow though like:
events.postUpdate("item", item+3);
danst
(Daniel)
January 28, 2022, 9:13am
3
Hi,
there is some real screw up in my code, now it looks like this:
logInfo("bewegungsmelder.rules", "EG_AZ Counter hoch: " +(EG_AZ_PRAESENZ_Counter.state as Number))
var tmp_count = EG_AZ_PRAESENZ_Counter.state
if (tmp_count < 1500) {
tmp_count = tmp_count + 3;
logInfo("bewegungsmelder.rules", "EG_AZ Counter hoch: " +(EG_AZ_PRAESENZ_Counter.state as Number))
EG_AZ_PRAESENZ_Counter.sendCommand(tmp_count)
logInfo("bewegungsmelder.rules", "EG_AZ Counter hoch: " +(EG_AZ_PRAESENZ_Counter.state as Number))
}
And the output is this, so the if-clause fails…
2022-01-28 10:12:14.019 [INFO ] [e.model.script.bewegungsmelder.rules] - EG_AZ Counter hoch: 113.80
rossko57
(Rossko57)
January 28, 2022, 9:35am
4
See -
This is doomed to failure.
Item states are not simple variables.
postUpdate() is asynchronous, the instruction to perform the update is sent away on openHABs events bus, triggering any rules or bindings that are interested. Eventually, it also updates the Item’s state - but it takes time. Just a few milliseconds.
Your rule does not stop and wait.
So when you read back the state in the same rule, you will almost certainly get the “old” state. Or you might be lucky, it’s indeterminate.
Th…
Unrelated - if you want to update an Item state, then update it. Less overhead than sending command, unless you have some binding or such to trigger.
rlkoshak
(Rich Koshak)
January 28, 2022, 3:10pm
5
danst:
What am I doing wrong?
You are expecting the Item to change state in response to the command immediately. Calling sendCommand or postUpdate generates an event. That event gets processed in the background. Your rule does not wait for that background processing to complete before moving on. So if you sendCommand or postUpdate to an Item and immediately pull the state from that same Item on the next line, it is almost always going to still be the old state.
Some time has to pass to give the event a chance to process and the Item change to the new state. So don’t rely on that. You’ve done the calculation in the rule. Use that calculation for the remainder of the rule instead of the Item’s state.
danst
(Daniel)
January 29, 2022, 7:20pm
6
Thanks, now I understand it better!
system
(system)
Closed
March 12, 2022, 11:21am
7
This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.