How to create a state machine with HA automation

Hi all,
this is my first post, most of my problem I could resolve by my own. I used in the past openHab for my automation. But now I moved to HA. And I have now 99 % running. Only one think I must move to HA. Maybe someone have a idea how.
I create a rules (in openHab) to get a message when my dryer is finished. And now I try to create the same with the automation in HA. I use a state machine in my old environemt. But current I habe no idea how to create this in HA.

For example the rule in openhab:

val String filename = "dryermachine.rules"
 
val Number MODE_OFF = 0
val Number MODE_ACTIVE = 1
val Number MODE_FINISHED = 2
 
rule "Dryer state: Init"
when
    System started
then
    if (dryer_OpState == NULL) {
        dryer_OpState.postUpdate(MODE_OFF)
        logInfo("dryer_OpState state" + dryer_OpState.state)
    }
end
 
rule "Washingmachine state: Power changed"
when
    Item hmSteckdose_CE_WASCHKUECHE_POWER changed 
then
    if (hmSteckdose_CE_WASCHKUECHE_POWER.state > 50) {
        dryer_OpState.sendCommand(MODE_ACTIVE)
    }
    else if (hmSteckdose_CE_WASCHKUECHE_POWER.state > 1.2 && hmSteckdose_CE_WASCHKUECHE_POWER.state < 50) {
        dryer_OpState.sendCommand(MODE_FINISHED)
        if(now.getHourOfDay() < 21 && now.getHourOfDay() > 8) {
            Echo_Kitchen_TTS.sendCommand('Trockner ist fertig!')
        }
    }
    else {
        dryer_OpState.sendCommand(MODE_OFF)
    }
end

The main idea is, that i measured the power consumption and know when the running programm is finished. The problem was that at the start the power consumption is also the same like at the end. This is why I create the state machine.
Any ideas how to do this in HA automation?

You could for example use input helpers to store information and use them in your Automations.
Or do it in node red, as there are state and statemachine nodes available as well as a Ha integration

That’s a good idea with the helper. Node-Red would be the second choice. :slight_smile:

But is there any option to organise my automations? Because after a while this could be confusing if there a to many automations.

In HA you need to trigger on the power consumption going to zero. Simple really.

How do you mean? Because the dryer always goes to a very low consumption during the program. But never to zero.
And so I always have the same state during operation as at the end of the program.

Not sure if I understand exactly what you need. How do you switch from MODE_FINISHED to MODE_OFF? Which means, how do you see if the dryer is emtpy and ready for the next run?

Maybe the delay_off option of the template binary sensor can help you if you have short “low power” times during a run. That’s how I see if the dryer or washing machine is running.
By that, you get the message a few minutes later, but that’s not a problem for me.
My dryer uses different power in state “finished but closed” and “off” => by that, I see if it’s finished or off.
The washing machine doesn’t have that difference. There I use a magnetic binary sensor (aqara) at the door to see when it gets opened.

Oh
 maybe I have a error in my old implementation. But it works. I will measure the power when I use it the next time, then I try to implement it in HA.
Thanks for the hints.

I’m using this binary_sensor to dect if my washingmashine is running:

  waschmaschine_running:
    delay_off:
      minutes: 3
    value_template: "{{ states.sensor.waschmaschine_ampere.state | float > 0.040 }}"

Maybe this helps :slight_smile:

1 Like

Almost the same here:

      waschmaschine_ist_an:
        friendly_name: "Waschmaschine lÀuft"
        delay_on:
          minutes: 3
        value_template: "{{ states('sensor.el_leistung_waschmaschine_scheinleistung')|float > 15 }}"
      trockner_ist_an:
        friendly_name: "Trockner lÀuft"
        delay_on:
          minutes: 1
        delay_off:
          minutes: 4
        value_template: "{{ states('sensor.el_leistung_trockner_scheinleistung')|float > 10 }}"

Just one small difference: according to


the usage of states(
) is preferred over the states.xyz.state as that one can give an error if the entity is not available (yet).
1 Like

Yes, thats right.
You should juse states(’
’) :slight_smile:

I am too new in the hole HA :thinking: . But what is a binary_sensor ? Is it the part I found -> “Bedingte Elemente”?
My target is to get an Alexa message if the dryer done his work. So, when “waschmaschine_running” is off. But do I not get every time a message if the dryer is off? I want only a message if the “waschmaschine_running” is changing from on to off.
Sorry for asking again.

A binary_sensor can be true or false. In your scenario it is on or off.

The trigger on the binary sensor fires when the binary_sensor reaches a certain state.
Example for your project:

alias: notify_wasche_fertig
trigger:
  - platform: state
    entity_id: binary_sensor.waschmaschine_running
    from: 'on'
    to: 'off'
action:
  - service: notify.mobile_app_ac2003
    data_template:
      title: WĂ€sche ist fertig!
      message: 'Hurti, Hurti, aufhÀngen, sonst machen stinki, stinki.'
  - service: notify.mobile_app_redmi_note_8
    data_template:
      title: '*WĂ€sche ist fertig!*'
      message: 'Hurti, Hurti, aufhÀngen, sonst machen stinki, stinki.'
  - service: notify.alexa_media
    data:
      data:
        method: all
        type: announce
      target:
        - media_player.Kuche
        - media_player.Wohnzimmer
        - media_player.Badezimmer
      message: 'Ey ihr nudeln, wÀsche ist fertig.'
initial_state: true
mode: single

OK. Im must add my binary sensors in the binary sensors yaml. And then I can use them in the automation. im was searching in the GUI for that. But must change it in the file. Understand

Slightly off topic: There is a third-party integration for HA called Appdaemon that enables you to write automation apps in python. I have implemented a general purpose finite state machine with appdaemon and so have others in this forum. This might be easier then trying to implement a state machine in the somewhat limited HA automation environment. https://appdaemon.readthedocs.io/en/latest/

1 Like

I would do what @nickrout said and use the power consumption going to 0 (or what ever the lowest value is when finished) for a period of time.

So if the cycles go to the lowest setting of power for (example) no longer than two minutes between the next cycle then set the trigger to:

trigger:
  - platform: numeric_state
    entity_id: sensor.dryer_power # or whatever the entity_id is for your dryer power monitor in HA
    below: 5 # or whatever the expected power level is when done
    for:
      minutes: 3 # longer than the longest interval between cycles so you know it's done if longer than that

I realise that you are new to HA so my apologies that I’m now giving you something extra to learn
 Have a look at Packages as these make organising your code SO much easier. It lets you keep all code related to a particular ‘thing’ or function in the one file. ie: clothes_dryer.yaml

I have basically what you want for my washing machine: this code is in config\packages\washing_machine.yaml

###  ALL CODE RELATED TO THE WASHING MACHINE   ###

input_select:
  washing_machine_state:
    name: Washine Machine is
    icon: mdi:washing-machine
    options:
      - 'Off'
      - Idle
      - Running
    initial: 'Off'

tplink:
  discovery: false
  switch:
    - host: 192.168.0.60

sensor:
  - platform: template
    sensors:
      washing_machine_power:
        friendly_name: "Washing Machine Power Consumption"
        value_template: '{{ states.switch.washing_machine.attributes["current_power_w"] | float }}'

### <2W = off        # Power readings at various states of the washing cycle for reference
### >3W = idle
### >7W = filling
### >90W = washing
### >200W = spin

automation:
  - alias: 'Washing maching -status tracker- Idle'
    trigger:
      - platform: numeric_state
        entity_id: sensor.washing_machine_power
        above: 2
        for: '00:00:05'
    action:
      - service: input_select.select_option
        data:
          entity_id: input_select.washing_machine_state
          option: Idle

  - alias: 'Washing maching -status tracker- Running'
    trigger:
      - platform: numeric_state
        entity_id: sensor.washing_machine_power
        above: 5
        for: '00:00:05'
    action:
      - service: input_select.select_option
        data:
          entity_id: input_select.washing_machine_state
          option: Running

  - alias: 'Washing machine -status tracker- Off'
    trigger:
      - platform: numeric_state
        entity_id: sensor.washing_machine_power
        below: 2
        for: '00:01:00'
    action:
      - service: input_select.select_option
        data:
          entity_id: input_select.washing_machine_state
          option: 'Off'

  - alias: 'Washing machine finished notification'
    trigger:
      - platform: state
        entity_id: input_select.washing_machine_state
        from: Running
        to: Idle
      - platform: state
        entity_id: input_select.washing_machine_state
        from: Running
        to: 'Off'
    action:
      - service: notify.mobile_phones
        data:
          message: 'The washing machine has finished'
          data:
            ttl: 0
            priority: high
5 Likes

I will try your hint. I include all this with my values and test it when I running my dryer the next time.

I am trying to figure this out but when i paste this in Developer template/editor i get this error

UndefinedError: ‘None’ has no attribute ‘attributes’

Have a look in the dev states panel to see that entity. Likely you will need to use the state rather than attribute. It’s just that your entity is different to mine.

i am using shellypm for this

sensor.shelly_shsw_pm_8caab505fc3b_current_consumption