Wow, a lot of work since I was last here! Thank you Seb, you’re awesome!
I have not updated yet, but I will – it’s time, now that I’m back writing scripts.
I have a question – a structural issue I’ve encountered ever since JSR223 days on OH2.4 when I first started.
On an ItemCommandEvent, if you get_value() the item that caused the event, you often get the old value, not the new value that caused the event.
If you look in the event object, the new value is there.
This is a huge headache when writing scripts where multiple items control the behavior of one thing.
For example (simple pseudocode example, obviously a real use case may have many more items controlling one thing)
def __init__:
self.itemPower=SwitchItem.get_item("switch")
self.itemPower.listen_event(self.update,ItemCommandEvent)
self.itemBrightness=DimmerItem.get_item("dimmer")
self.itemBrightness.listen_event(self.update,ItemCommandEvent)
def update(self, event):
power=self.itemPower.get_value()
bright=self.itemBrightness.get_value()
if power == 0:
bright = 0
send_mqtt(light_topic,f"dimmer={bright}")
You may find that the light turns off when it’s supposed to turn on etc.
You can of course add the following to update():
if name=="switch":
power=event.value;
if name=="dimmer":
bright=event.value
…and that is what I’ve done so far in my scripts, but this is messy and adds a lot of extra work and code, for something I feel could be handled upstream, to simplify script development!
I understand that the openHAB event bus is asynchronous and multithreaded and that this is the root cause of the issue, but …
can’t HABApp work around this issue by caching the new value from the event callback so that when I call get_value(), I get the current value instead of an out of date value?
That would really simplify things.
Is this by any chance you’ve already taken care of? Any suggestions?