Any binary sensor "on_state" or "on_exec" functionality for scripts and scenes?

Hi,

I’m currently building a control panel with an esp32 and a touch screen (using lvgl)

I just populate this screen with differents buttons to control light swicth, but also to execute scenes and scripts…

Currenlty, for scenes and scripts buttons, i update the state of each button “on press”

Example, if i press button 17, i turn_on the corresponding scene then set the state of button 17 on, and the state of button 18 off :

My problem : if i execute the scene/script from another device (HA dashboard, smartphone App,…), that does not update my control panel buttons state…

For light switch buttons, i update the state of the buttons with a binary_sensor “on_state”, but how can i achieve the same behavior for scenes and scripts ? Is there any sensor “on_execute” for scenes and scripts that i could use in my code ?

If you have any idea, thanks to let me know :slight_smile:

Some examples of my current ESP32 scripts

Update light switch button

binary_sensor:
  - platform: homeassistant
    id: btn01_state
    entity_id: ${btn01_entity}
    publish_initial_state: true
    on_state:
      then:
        lvgl.widget.update:
          id: btn01
          state:
            checked: !lambda return x;

Click and update Scene button

              - buttons:
                - id: btn17
                  text: ${btn17_label}
                  control:
                    disabled: ${btn17_disabled}
                    hidden: ${btn17_hidden}
                  on_click:
                    then:
                      - homeassistant.action:
                          action: scene.turn_on
                          data:
                            entity_id: ${btn17_entity}
                      - lvgl.widget.update:
                          id: btn17
                          state:
                            checked: true
                      - lvgl.widget.update:
                          id: btn18
                          state:
                            checked: false

I don’t quite understand what your requirements are, but in Jinja templating syntax checking whether a light is on or a script is executing is the same, is_state(). Scenes however are stateless, there is currently no built-in way to check whether a scene is on not. There are some third-party implementations of scenes that adds state.

I would like to trigger some code on my esp32 when a script is executed on HA or when a scene is activated, to modify the state of some buttons on the esp32 display…

I have an event “on_state” catching when light switch state change, but i would like to have a similar event like “on_execute” when a script is executed, and “on_activate” when a scene is activated…

I’ve gived the example of the script i use with light switches “on_change”, i would like to have exactly the same functionality but for scenes and scripts…

I do not need the current scene/script “state” (i know there is no state for thoses), i just need to trigger an event when those are executed/activated

Currently i’ve created a virtual input_boolean that i modify in my scenes and scripts, those input_boolean can trigger the on_state event in my esp32, but it’s really not a clean solution to create a virtual input_boolean for each scene/script…

Again, it is very unclear to me if whatever code you are running reacts to an event or a change of state, and if the latter whether that requires changing to a specific state or to any state.

If you need an event, listen for a script_started event. There is no exact equivalent for scenes, but their state is the datetime (as a string) when last applied, so you could listen for a state_changed event if you can configure one that works for your exact desired parameters, or create an automation that on a change of state sends a custom event.

If you can react to a change of state, a script would be identical to a light. Again scenes are different, as their state is a datetime string, so if you can only react to a change of state into a specific value like “on” you may need to setup an intermediate binary_sensor.