I updated the code to update the AD internal state machine to reflect changes to the input_select that mirrors the state machine’s state. So now, if I change the value of the input_select.washing_machine_status dropdown in the UI, the AppDaemon code will notice it and update its internal state.
The template entities I showed above are defined in HA, not in AD.
In general, the idea is that Home Assistant is the authoritative source of information.
Nice work. I like the idea of the state machine even as @ReneTode said we have been doing this with auxiliary input_selects on HA, appdaemon or node-red. You provided another choice and I thank you for that. Now, I’d like to make a suggestion. Usually states machines sounds complex to the average user and doing it by code just add to this. As @MatthiasU just suggested while I was writing this, providing a simpler way to just write yaml would be great.
Another thing to think about (which is just a special case of a state machine) is how to implement event sequences. I have a few cases which this would come handy. For example: if movement is detect on sensor1 and then on sensor2 the person is moving to place x so do something.
I’m interested why it didn’t work? I didn’t have any issues. The threaded nature of app daemon might cause problems, though I haven’t run into any just yet.
I had trouble transforming what I wanted to express (when this state changes from ‘on’ to ‘off’, transition from state A to state B) into the concepts of transitions, especially that transitions uses strings to identify triggers that then generate methods. My goal was to hide the listen_state() and run_in() code inside the library.
@danny, do you have a code sample you can share that uses transitions with AppDaemon?
With state machines you need to think of it in terms of events. Given that I am in state A, what do I have to do when event STATE_CHANGE_ON occurs?
I had trouble converting my if/else logic to states.
I tried replicating your lights logic using my library:
It is a very simplified version. I read through your code and there is a lot of customization that I omitted in this snippet.
In general, using my library would save you the need to handle the timer and subscribing to state changes and triggering state transitions based on them. There is a lot of customization code that would stay the same.
i need to do some more testing, but i am close to releasing it.
i made it even possible to use other languages, or other words.
so an automation like this:
I like this very much.
Some improvements to consider:
A special command when a state is entered (whichever transition is taken)
A special command when a state is exited (whichever transition is taken)
This helps some state machines where actions are taken based on state, rather on the transition.
Only take a transition if the condition has been met for a certain time (stability)
And finally a question:
Can you add an example showing a transition to be taken only if both condition A and B are met?
Thank you very much. Look forward to when this is released!
I think I have found a bug. When there is a transition with timeout, no other transition (without timeout) is considered until the timer expires.
In this graph it seems impossible to do: Start -> On -> Off. It takes a full 30s between On and Off.
Edit: My problems disappeared when I stopped using this syntax: add_transitions([State1, State2, State3], trigger, new_state)
and instead had one state only add_transitions([State1], trigger, new_state) add_transitions([State2], trigger, new_state) add_transitions([State3], trigger, new_state)
It also worked like this: add_transition(State1, trigger, new_state) add_transition(State2, trigger, new_state) add_transition(State3, trigger, new_state)
I could not pinpoint the problem, but I added some printouts, and for some reason not all transitions where tried with this syntax: add_transitions([State1, State2, State3], trigger, new_state)