Behavior of ItemStateUpdatedEvent

A basic question of behavior.

when ItemStateUpdatedEvent trigger is enabled for all changes:

self.listen_event('iPhoneIVTPingOK', self.on_all, EventFilter(ItemStateUpdatedEvent)) 

the event triggers for all events even when there is no change of value, not only when there is an ald/new value.

[2023-08-15 09:30:02,023] [          HABApp.EventBus]     INFO |      iPhoneIVTPingOK: <ItemStateUpdatedEvent name: iPhoneIVTPingOK, value: ON>
[2023-08-15 09:30:02,027] [          HABApp.EventBus]     INFO |      iPhoneIVTPingOK: <ItemStateChangedEvent name: iPhoneIVTPingOK, value: ON, old_value: OFF>
[2023-08-15 09:30:02,059] [          HABApp.EventBus]     INFO |      iPhoneIVTPingOK: <ItemStateUpdatedEvent name: iPhoneIVTPingOK, value: ON>
[2023-08-15 09:30:06,201] [          HABApp.EventBus]     INFO |      iPhoneIVTPingOK: <ItemStateUpdatedEvent name: iPhoneIVTPingOK, value: ON>
[2023-08-15 09:30:06,742] [          HABApp.EventBus]     INFO |      iPhoneIVTPingOK: <ItemStateUpdatedEvent name: iPhoneIVTPingOK, value: ON>

Is this a wanted behavior?
I expected only when there is an old/new value, regardless of which values old/new has.

thats what the commands name suggests: itemstateUPDATEDevent. if your item is ON and you postupdate ON again this will trigger. there are reasons when this is important to work like that.

if you only want to trigger if the item changes use EventFilter(ItemStateChangedEvent)

1 Like

Or directly the ItemStateChangedEventFilter

1 Like

@bastler , @Spaceman_Spiff Thank you for you answers

Yes,

self.listen_event('iPhoneIVTPingOK', self.on_all, EventFilter(ItemStateChangedEvent)) 

behaves as expected and as I need for evaluation of state change.

True

I did not know how to use these filters and asked GPT4:

give me an example of how to use these habapp classes:

# Todo: Drop this when we go OH4.0 only
class ItemStateEventFilter(TypeBoundEventFilter):
    def __init__(self, value: Any = MISSING):
        super().__init__(ItemStateEvent, value=value)


class ItemStateUpdatedEventFilter(TypeBoundEventFilter):
    def __init__(self, value: Any = MISSING):
        super().__init__(ItemStateUpdatedEvent, value=value)


class ItemStateChangedEventFilter(TypeBoundEventFilter):
    def __init__(self, value: Any = MISSING, old_value: Any = MISSING):
        super().__init__(ItemStateChangedEvent, value=value, old_value=old_value)


class ItemCommandEventFilter(TypeBoundEventFilter):
    def __init__(self, value: Any = MISSING):
        super().__init__(ItemCommandEvent, value=value)

And got this answer:

Okay, understanding that these classes are from HABApp (which facilitates working with OpenHAB in a Pythonic way), the usage would be tied to how HABApp handles events and rules.

HABApp creates a straightforward way to listen to OpenHAB events and react accordingly using Python. The event filters you provided allow you to fine-tune which events you want to listen for.

Here’s a basic example of how you might set up a rule using these event filters in HABApp:

import HABApp
from HABApp.core.events import ItemStateEvent, ItemStateUpdatedEvent, ItemStateChangedEvent, ItemCommandEvent
from HABApp.core.items import Item
from .your_event_filters_module import ItemStateEventFilter, ItemStateUpdatedEventFilter, ItemStateChangedEventFilter, ItemCommandEventFilter

class MyRule(HABApp.Rule):

    def __init__(self):
        super().__init__()

        # Create filters
        self.state_filter = ItemStateEventFilter(value="ON")
        self.update_filter = ItemStateUpdatedEventFilter(value="OFF")
        self.change_filter = ItemStateChangedEventFilter(value="OFF", old_value="ON")
        self.command_filter = ItemCommandEventFilter(value="TOGGLE")

        # Listen for events
        self.listen_event('SomeOpenHABItem', self.item_turned_on, self.state_filter)
        self.listen_event('AnotherOpenHABItem', self.item_updated_off, self.update_filter)
        self.listen_event('YetAnotherOpenHABItem', self.item_changed_from_on_to_off, self.change_filter)
        self.listen_event('FinalOpenHABItem', self.item_received_toggle_command, self.command_filter)

    def item_turned_on(self, event: ItemStateEvent):
        self.log.info(f"Item {event.item_name} is now ON")

    def item_updated_off(self, event: ItemStateUpdatedEvent):
        self.log.info(f"Item {event.item_name} was updated to OFF")

    def item_changed_from_on_to_off(self, event: ItemStateChangedEvent):
        self.log.info(f"Item {event.item_name} changed from ON to OFF")

    def item_received_toggle_command(self, event: ItemCommandEvent):
        self.log.info(f"Item {event.item_name} received a TOGGLE command")


# Register the rule to make it run
MyRule()

This example assumes that the event filters are defined in a separate module, which I’ve named .your_event_filters_module for the sake of the example. The way these filters work is they listen for events on specific OpenHAB items and then trigger specified functions in response.

Again, this is a basic demonstration to give you an idea. You may need to adjust based on your specific requirements and the latest capabilities of HABApp.

This is pretty amazing!

instead of

self.listen_event('iPhoneIVTPingOK', self.on_all, EventFilter(ItemStateUpdatedEvent))

you could write

self.listen_event('iPhoneIVTPingOK', self.on_all, ItemStateChangedEventFilter())

of course then dont forget to

from HABApp.openhab.events import ItemStateChangedEventFilter
1 Like

@lukics You should really ask the docs instead of ChatGPT

Non the less it’s impressive that it also can answer questions to such a niche library even when the answer is only 70% correct.

1 Like

Unfortunately the search is still not working and the stepping throgh the menues is s pain.

And yes. 70% is realy impressive but enough to get the idea