Best way to setup a state machine via automations?

Hey folks - I’ve setup HA after a year or two away when I moved. I’m using HA Core + Docker (so I don’t have the easiest access to integrations, but I’m open to shifting if there is a good case to do so).

I know the team made a ton of progress on automation. What’s the current best practice for creating a simple state machine who’s state is displayed in the interface? I know Home-Red but dearly hoping I don’t need to spin it up given all the progress we’ve had on HA.

I’m chasing the old hobbyhorse of “dishwasher state” with a simple vibration sensor (it has good sensitivity and resets “Detected” to “Clear” every ~5 seconds):

  • 10+ minutes of vibration triggered = “Running” state
  • No vibration for 10 min after Running state = “Done” state
  • First short vibration after Done state= “Opened/Unloaded” state
  • Any vibrations atleast 4 hours after Opened/Unloaded state = “To be run” state

This just sounds like an automation that triggers off the state of a vibration sensor automating an input select helper that has these states.

1 Like

That sounds easy! Do I need to setup up the input select helper, then write the automation? Does HA have a state diagram tool or something to help?

create the input select helper then write the automation: yes
state diagram tool: no
something to help: yes - community.home-assistant.io - you should check it out :wink:

1 Like

Drat I was hoping HA had some of these basics covered!

Do you have a recommended state diagram-ish tool or best practice? Obviously I’ve checked the community, and there isn’t a best practice easily found. Everybody does it a different way :frowning:

Why do we insist on reinvention of the wheel? Ah well.

Just make a flow chart or list for what you want to do.

You already have the list above.
Then open the automation UI and do it.

i’d just do it in direct automation. it’s not at all hard.
i don’t think there’s an issue of reinvestion of the wheel, it’s more a question of prioritization… of all the things that the team could work on, is this the best leveraged? i’d argue “no”.

but hang on a sec, and i’ll throw a sample together for you…

1 Like

these can easily be combined into 1, but i’m doing them separately in part because it shows how directly and easily the description you put above translates to the code:

1: setting the “running” state

description: "set running state"
trigger:
  - platform: state
    entity_id:
      - sensor.dishwasher_vibe
    to: "on"
    for:
      minutes: 10
condition: []
action:
  - service: input_select.select_option
    data:
      option: running
    target:
      entity_id: input_select.test_dropdown

2: setting the done state

description: "set done state"
trigger:
  - platform: state
    entity_id:
      - sensor.dishwasher_vibe
    to: "off"
    for:
      minutes: 10
condition:
  - condition: state
    entity_id: input_select.test_dropdown
    state: running
action:
  - service: input_select.select_option
    data:
      option: done
    target:
      entity_id: input_select.test_dropdown

3: setting the opened state

description: "set opened state"
trigger:
  - platform: state
    entity_id:
      - sensor.dishwasher_vibe
    to: "on"
condition:
  - condition: state
    entity_id: input_select.test_dropdown
    state: done
action: 
  - service: input_select.select_option
    data:
      option: opened
    target:
      entity_id: input_select.test_dropdown

of course, switch my made up entities with your real ones…

1 Like

This is super kind of you! Thank you!

Very helpful template to build off.

You mentioned this can be combined into one automation - what’s the best way to do that? (Don’t need a full example, just a pointer so I can dig into the docs).

doing the 4th is a very simple pattern match from the rest… i’m leaving it as an exercise for you because it’s simple and if you’re gonna be in this ha world, you shouldn’t be afraid of such simple automations…

or maybe someone else will come along and just hand you the last fish… but i hope that handing you 3 and having you figure out how to do the 4th is the best thing…

1 Like

i’d recommend you do the 4th and have it all working before you try to combined.

but the way to combine them is to do a trigger id. to have all the triggers together (an automation can have many different triggers)… assign an id for each trigger. then do the corresponding thing depending on which trigger is fired. it’s not really any better than doing 4 different atomic automations like this. but some people like to have just 1… even if it’s more complex.

again, if this is your first go writing automation, i’d strongly encourage you to get the 4 working independently, then work to combine them if you want…

Thanks - I did a bunch of these a few years ago, but syntax etc. often changes. Super helpful! I have it up and running. Def want to integrate into once automation though. Will play with this trigger id thing.

oh… you asked for docs about how to combine. look at trigger id here:

1 Like

This didn’t exist the last time I fooled w HA! Appriciate it.

Man … we really need a good written guide to best practices for common HA tasks. Wild that it’s all somewhere deep in various forum discussions (of varying ages!)

we have a lot of them here: Community Guides - Home Assistant Community

however the reality is that ha is soo flexible that there are a ton of common ha tasks. many of them are consolidated into blueprints (e.g. motion detection)…

here’s one that’s used for dishwasher and washing machines: 🔌 Detect and monitor the state of an appliance based on its power consumption - V2.1.1 - Updated! - #4 by siklosi

i think if you asked the community for what their common tasks are, i bet you’d get hundreds of passionately different answers…