[Solved] postUpdate not synchronous. Is it on purpose?

postUpdate and sendCommand are processed asynchronously. You cannot expect the state of an Item you just postUpdate to to reflect the new state immediately. It takes some milliseconds for the update to get processed round trip.

That’s fine, but if you change the Item in a Rule, you already know what it was changed to. So just use a local variable inside the Rule. If other Rules or future runs of this same Rule need the data too, then also publish the data as an update to the Item.

Using Items to store variables makes good sense when that variable needs to be preserved over multiple runs of the Rule or across multiple Rules. If the value is only used by the one run of the one Rule the using an Item is a bad idea. Use a local variable instead.

It does not work like that and frankly if it did work like that it would be close to a disaster for other parts of how Rules work.

I’m not recommending this as a solution, but you could also add a Thread::sleep after you call postUpdate, but that can cause problems as well.

So your proposal of using local variables and then calling postUpdate is probably your best bet.

1 Like