`listen_state`: how to use a lambda with `new`?

I am trying to run a callback whenever an entity is not in a certain state.

This is the entity: input_boolean.debug_switch_3
I want the listen_state to fire whenever it is not in state on (for example).

I have this code:

self.listen_state( self._wait_for_cb, entity=entity, new=lambda x: x != "on" )

I’d expect this to run the callback _wait_for_cb to be called whenever the state of input_boolean.debug_switch_3 changes to something that is not on.

What is happening though is that the callback is called whenever any entity has a statechange to anything but on:


2026-01-16 17:13:42.352969 INFO adsequence: sequencetest: wait_for_callback has been activated for entity sensor.wohnzimmer_aussenschalter_power_b
2026-01-16 17:13:42.357072 INFO adsequence: sequencetest: wait_for_callback has been activated for entity sensor.wohnzimmer_aussenschalter_voltage
2026-01-16 17:13:42.361528 INFO adsequence: sequencetest: wait_for_callback has been activated for entity sensor.wohnzimmer_aussenschalter_frequency

Am I using the lambda wrong? Or is it just not supposed to work the way I expect it to work?

Any help is appreciated! :slight_smile:

I’ve not used a lambda there but in the documentation it has an example:

self.handle = self.listen_state(
    self.my_callback,
    "light.office_1",
    new="on",
    old=lambda x: x.lower() not in {"unknown", "unavailable"}
)

You might try and adjust the example’s lambda to:

new=lambda x: x.lower() in {"unknown", "unavailable"}

or maybe using “old” is better?

old=lambda x: x.lower() in {"unknown", "unavailable"}
1 Like

Sometimes you need a second set of eyes to tell you to read the documentation again. Thank you very much! I used entity=... instead of entity_id=, like so:

self.listen_state( self._wait_for_cb, entity_id=entity, new=lambda x: x != "on" )

Though I really like the idea of using old=..., I might use that instead.

Thanks again!