Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
samturner3 opened this issue Nov 18, 2022 · 3 comments
Open
Labels
enhancement New feature or request

Comments

@samturner3
Copy link

samturner3 commented Nov 18, 2022

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 samturner3 changed the title Ability to directly set state (from HA) Ability to directly set state (from HA) WITH on_enter automation run Nov 18, 2022
@muxa
Copy link
Owner

muxa commented Nov 18, 2022

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 muxa added the enhancement New feature or request label Nov 22, 2022
@samturner3
Copy link
Author

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

@pzeinlinger
Copy link

That would be great! Any news?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants