Blockly logs old values

  • Platform information:

    I am running Openhabian 4.0.3 on Raspberry Pi 4 8GB

I have a simple blockly script where I change the value of an item and then save it to a Variable.

blocklyA

One would expect to see the number 5 in the logs.
Instead i get 7 (which is the previous value of the item).

2023-09-17 11:27:21.908 [INFO ] [nhab.automation.script.ui.5ae1688421] - Script Started
2023-09-17 11:27:21.911 [INFO ] [nhab.automation.script.ui.5ae1688421] - 7
==> /var/log/openhab/events.log <==
2023-09-17 11:27:21.911 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'TestNumberItem' received command 5
2023-09-17 11:27:21.913 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'TestNumberItem' received command 7
2023-09-17 11:27:21.914 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'TestNumberItem' changed from 7 to 5
2023-09-17 11:27:21.917 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'TestNumberItem' changed from 5 to 7

Now if i put a small delay after sending a new value to the items the script works correctly.

blocklyB

2023-09-17 11:30:46.750 [INFO ] [nhab.automation.script.ui.5ae1688421] - Script Started
==> /var/log/openhab/events.log <==
2023-09-17 11:30:46.752 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'TestNumberItem' received command 5
2023-09-17 11:30:46.754 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'TestNumberItem' changed from 7 to 5
==> /var/log/openhab/openhab.log <==
2023-09-17 11:30:47.753 [INFO ] [nhab.automation.script.ui.5ae1688421] - 5
==> /var/log/openhab/events.log <==
2023-09-17 11:30:47.755 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'TestNumberItem' received command 7
2023-09-17 11:30:47.757 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'TestNumberItem' changed from 5 to 7

Any ideas?

Yes, you have to post an update not send a command. A command “indirectly” updates an item, a post does it directly.

postUpdate is used to update the state of an item without triggering any associated bindings or sending commands to the device or service it represents.

sendCommand is used to send a command to an item, and it often results in a change in the state of the item.

which is not related to blockly.

However, please read the blockly docs that link to the respective docs that explain the differences:

https://www.openhab.org/docs/configuration/blockly/rules-blockly-items-things.html#send-command

and therefore

I hope that helps

1 Like