A better way to do this automation?

So I have a pump at a weekend house that I want to turn on if I’m there, after 5AM and off again after 23:00.

I have a presence thing that reliably detects when I’m “home” (at the weekend house) and can turn on the relay. Unfortunately, it only triggers when the state changes. So if for some reason, I restart HA, the state comes up “home” so the pump doesn’t turn on. It looks like this:

  • id: recirc_on
    alias: Turn on recirculation pump after 5AM, if we’re here
    initial_state: ‘on’
    trigger:
    • platform: state
      entity_id: device_tracker.tr60
      to: ‘home’
      condition:
    • condition: time
      after: ‘05:00:00’
    • condition: time
      before: ‘23:00:00’
      action:
    • service: switch.turn_on
      entity_id: switch.recirculation_pump`

I’ve changed it to run every 5 seconds (which I know is short but I’m developing a component so I’m doing a lot of restarting) but this way it checks the current state of device_tracker.tr60 and set the pump accordingly. The drawback is it runs a lot, but it also sends an MQTT message to turn on the pump very frequently (though not every 5 seconds, curiously):

  • id: recirc_on
    alias: Turn on recirculation pump after 5AM, if we’re here
    trigger:
    • platform: time
      seconds: 5
      condition:
      condition: and
      conditions:
      • condition: state
        entity_id: device_tracker.tr60
        state: ‘home’
      • condition: time
        after: ‘05:00:00’
        before: ‘23:00:00’
        action:
    • service: switch.turn_on
      entity_id: switch.recirculation_pump

Is there some way to set the initial state of the device tracker to ‘away’ so that it’ll cascade all of my dependent automations to do the right thing on startup? Or is there a better way to do this automation?

Set a condition that’s says the pump is off.

That way the automation will only run if the pump is not already on.

That would be my approach.

do you only reboot when you are there? Then maybe another automation that sets it to on when Hass starts.

Unfortunately, it’s a house deep in the forest so the power goes out frequently and at times for longer than my UPS can last…

I’m not quite sure I understand. Where does the condition get set? I assume you mean the initial state of the automation should be ‘off’ and then some other event turns the automation on?

Then maybe trigger on Hass start and condition if you’re “Home”. Or does it always say your home on start? Even when you are not?

1 Like

Add a condition to your automation that says.

  - condition: state
    entity_id: switch.recirculation_pump
    state: 'off'

So every five seconds the automation will trigger but because the pump is already on the automation will not fire the actions.

That’s a good idea actually. I just did added a new automation that triggers on homeassistant start that turns on the pump if we’re “home” and between the correct times.

rabittn: I see what you’re doing there. Yes, that would work. I do like ‘teachingbirds’ solution better.

Thanks to both of you for the help.

1 Like

No problem glad I could help.

Another option would be template triggers if you did not want to create multiple automations.

1 Like