Adding to an Number Item

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);

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

See -

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.

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.

Thanks, now I understand it better!

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.