esphome-state-machine icon indicating copy to clipboard operation
esphome-state-machine copied to clipboard

Ability to directly set state (from HA) WITH `on_enter` automation run

Open samturner3 opened this issue 3 years ago • 3 comments

On state_machine.set Action docs:

Note that only the target state on_set automation will be triggered, and all other state machine automations (on_enter, on_leave and action of the inputs and transitions) will be skipped.

It would be great if we could have an option to directly set the state but still activate (at least) the on_enter, of the inputs and transitions.

The use case here is a state machine with many complex states and transitions for a light switch with PIR motion sensor and led indicator light per switch. I want to push a HA dashboard button to set a mode/state directly. ie turn off motion mode, etc ect.

Without the above I would have to write complex logic in the device (using state_machine.state etc) to first check what the current state of the machine is, and determine what sequence of transitions I need to action to arrive at the desired new state. It is like I have to write all possible state transitions again, not being DRY, and breaking when state machine logic is changed.

I need the on_enter automation to run as it sets the light, sets the indicator led to show the mode etc etc.

I cannot easily just use the state_machine.transition Action in my case as the transition action would be dependent on the current state.

Currently I guess an easier way would be to have more simple logic determining the state_machine.transition Action required to reach the desired new state based on the current state, however this may sill break if the state machine logic is updated, and would still be complex on complex state machines.

For reference this is my project: https://community.home-assistant.io/t/australian-light-switch-with-motion-sensor-local-control-show-and-tell/444612

samturner3 avatar Nov 18 '22 01:11 samturner3

Yes, I think having an option to call on_leave and on_enter when changing state via set would be useful. Perhaps add flags to the set call? Something like this:

  - state_machine.set:
      state: OPEN
      call_on_leave: true
      call_on_enter: true

muxa avatar Nov 18 '22 23:11 muxa

In the meantime I have a work around by adding more inputs HA_MODE_CHANGE_TO_MANUAL and HA_MODE_CHANGE_TO_AUTO:

- name: HA_MODE_CHANGE_TO_MANUAL
        transitions:
          - from: OFF_STANDBY
            to: OFF_MANUAL
          - from: OFF_STANDBY_TIMEOUT
            to: OFF_MANUAL
          - from: ON_STANDBY_0
            to: ON_MANUAL
          - from: ON_STANDBY_1
            to: ON_MANUAL
          - from: ON_STANDBY_2
            to: ON_MANUAL
      - name: HA_MODE_CHANGE_TO_AUTO
        transitions:
          - from: ON_MANUAL
            to: ON_STANDBY_0
          - from: OFF_MANUAL
            to: OFF_STANDBY
switch:
  - platform: custom
    lambda: |-
      auto my_custom_switch = new MyCustomSwitch();
      App.register_component(my_custom_switch);
      return {my_custom_switch};
  
    switches:
      name: "Switch manual mode"
      id: ${ha_auto_switch_1_id}
      on_turn_off:
        - state_machine.transition:
            id: sm1
            input: HA_MODE_CHANGE_TO_AUTO
      on_turn_on:
        - state_machine.transition:
            id: sm1
            input: HA_MODE_CHANGE_TO_MANUAL

samturner3 avatar Nov 23 '22 03:11 samturner3

That would be great! Any news?

pzeinlinger avatar Jul 07 '23 09:07 pzeinlinger