How to prevent automation from running after restart?

I have an automation that is triggered once my energy consumption goes to 0. The problem is: whenever I restart the pod this automation is triggered once more.

I found some old posts about using home assistant uptime however soon I realized that this happens also when I restart mosquitto so I need a better way to do that.

After playing around with the configuration I found a way that seems to be working:

  • Create an Boolean input
  • Create an automation that set input to true once the energy consumption goes above 0
  • Add condition to my automation where input should be true and add an action to set it to false in the end of the automation

IMO this looks like a massive work for a typical use case so I wonder if there’s anything better. Does anyone have a better way to prevent the automation from running after restart?

1 Like
  trigger:
    - platform: state
      entity_id: sensor.my_sensor
      not_from:
        - "unknown"
        - "unavailable"
      to: "0"
5 Likes

@Edwin_D thanks!!! You are the best

1 Like

This no longer works. Results in a “Message malformed: extra keys not allowed @ data[‘not_from’]” error

Can you post more of the code where you put it in? Because it should still work in a trigger. Won’t work in a condition though.

I get this issue in the visual editor when i am trying to add this slice of code in the when section

full yaml

alias: Washing Completed
description: ""
trigger:
  - type: power
    platform: device
    device_id: 9175431c24d6e742e26ea1c89decf9e1
    entity_id: sensor.washing_machine_power
    domain: sensor
    below: 1
    for:
      hours: 0
      minutes: 5
  - platform: state
    entity_id: sensor.washing_machine_power
    not_from:
      - unknown
      - unavailable
    to: "0"
condition:
  - condition: time
    after: "07:00:00"
    before: "23:00:00"
    weekday:
      - sat
      - fri
      - thu
      - wed
      - tue
      - mon
      - sun
action:
  - service: notify.notify
    data:
      message: Washing Complete
  - service: tts.google_say
    data:
      entity_id: media_player.leftwingofhouse
      message: Washing is Done
mode: single

It’s not an issue. It’s just letting you know that the visual editor doesn’t have a way to display it for the time being.

You can still add/edit it in yaml mode and it’ll work fine, just like it tells you on the third line.

Ok thanks - but the automation still runs after a home assistant restart. It is not working.

Why do you have a device trigger and a state trigger? Get rid of the device trigger. I suspect that is what is triggering upon a restart.

You can add the for: block to your state trigger if you want.

Try this. I’ve been using this automation reliably for the same scenario.

alias: Washing Completed
description: ""
trigger:
  - below: 1
    entity_id:
      - sensor.washing_machine_power
    platform: numeric_state
    for:
      hours: 0
      minutes: 5
      seconds: 0
condition:
  - condition: template
    value_template: "{{ trigger.from_state.state not in ['unknown', 'unavailable'] }}"
  - after: "07:00:00"
    before: "23:00:00"
    condition: time
action:
  - service: notify.notify
    data:
      message: Washing Complete
  - service: tts.google_say
    data:
      entity_id: media_player.leftwingofhouse
      message: Washing is Done
mode: single

The ordering of the trigger parameters looks messed up (platform: comes after below:) because I initially set it up in the UI. It’s a bit confusing when you’re used to a certain sequence of parameters, but it works just fine.

2 Likes

this worked. thank you so much!

1 Like