Time Trigger 4 times a day for automation

Hi,
I need your help. To run a filtration pump several times a day for one hour, I need automation. So far I’ve created four automations for it, but I think it’s certainly easier.

- id: poolpumpe_an
  alias: Poolpumpe an Automation
  initial_state: 'on'
  condition:
  - condition: state
entity_id: input_boolean.automation_poolpumpe
state: 'on'
  trigger:
platform: time
at: '8:00:00'
  action:
  - service: switch.turn_on
entity_id:
- switch.poolpumpe
- id: poolpumpe_aus
  alias: Poolpumpe aus Automation
  initial_state: 'on'
  condition:
  - condition: state
entity_id: input_boolean.automation_poolpumpe
state: 'on'
  trigger:
platform: time
at: '9:00:00'
  action:
  - service: switch.turn_off
entity_id:
- switch.poolpumpe

Can you give me a hint?

You could do a time_pattern trigger to start a timer. How many times a day does it need to run? Is there a time of day it cannot run?

First automation turns on the pump four times during the day.

- id: poolpumpe_an
  alias: Poolpumpe an Automation
  trigger:
  - platform: time
    at: '09:00:00'
  - platform: time
    at: '12:00:00'
  - platform: time
    at: '18:00:00'
  - platform: time
    at: '21:00:00'
  condition:
  - condition: state
    entity_id: input_boolean.automation_poolpumpe
    state: 'on'
  action:
  - service: switch.turn_on
    entity_id: switch.poolpumpe

Second automation turns off the pump after it has been on for 1 hour.

- id: poolpumpe_aus
  alias: Poolpumpe aus Automation
  trigger:
  - platform: state
    entity_id: switch.poolpumpe
    to: 'on'
    for: '01:00:00'
  condition:
  - condition: state
    entity_id: input_boolean.automation_poolpumpe
    state: 'on'
  action:
  - service: switch.turn_off
    entity_id: switch.poolpumpe
2 Likes

@123 - what would happen if HA is restarted while the pump is running?
Does the ‘for’ trigger still count until the hour is over or would it run until the next on trigger comes around or would the pump run for 1h after the reboot?

as long HA knows that the switch.poolpumpe has the state ’ on ’ yes
‘worst’ thing that can happen is that the pump is running for ~ 01:59:40 hours (when the restart is timed right)

Long story short, it’ll screw it up. Timers in general don’t survive a restart and for is basically an unseen timer.

Simple test:

- alias: 'scheduled light on'
  trigger:
  - platform: time
    at: '13:55:00'
  action:
  - service: light.turn_on
    entity_id: light.family


- alias: 'scheduled light off'
  trigger:
  - platform: state
    entity_id: light.family
    to: 'on'
    for: '00:05:00'
  action:
  - service: light.turn_off
    entity_id: light.family
  • Light turned on, as expected, at 13:55 (should turn off 5 minutes later at 14:00).
  • Restart Home Assistant at 13:58.
  • Light failed to turn off at 14:00.
  • Light remains off ever after (i.e. does not get turned off 5 minutes past 13:58).

So, if I want to be on the safe side I should probably use the following as a 3rd automation - with the one you wrote as the 2nd one as a fallback in case something strange happens:

- id: poolpumpe_aus
  alias: Poolpumpe aus Automation
  trigger:
  - platform: time
    at: '10:00:00'
  - platform: time
    at: '13:00:00'
  - platform: time
    at: '19:00:00'
  - platform: time
    at: '22:00:00'
  - service: switch.turn_off
    entity_id: switch.poolpumpe
1 Like

Explicitly specifying the 'off time` is probably the most conservative and reliable way of avoiding scheduling problems that may occur after a restart.

Given Home Assistant’s inability to preserve timers (including the invisible one used with for) after a restart, if someone wanted total reliability they should probably avoid using them. However, that removes some useful tools from the toolbox.

If you intend to specify the precise ‘off time’ then there’s no need to use the for statement as I’ve shown. In other words, 2 automations is sufficient and there’s no need for 3 of them.

Thank you very much.
To be on the safe side, I will use all three automations. It’s better to do too much, than too little…

My solution now:

- id: poolpumpe_an
  alias: Poolpumpe an Automation
  trigger:
    - platform: time
      at: '07:00:00'
    - platform: time
      at: '12:00:00'
    - platform: time
      at: '18:00:00'
    - platform: time
      at: '21:00:00'
  condition:
  - condition: state
    entity_id: input_boolean.automation_poolpumpe
    state: 'on'
  action:
  - service: switch.turn_on
    entity_id: switch.poolpumpe

- id: poolpumpe_aus_trigger
  alias: Poolpumpe aus Automation Trigger
  trigger:
  - platform: state
    entity_id: switch.poolpumpe
    to: 'on'
    for: '01:00:00'
  condition:
  - condition: state
    entity_id: input_boolean.automation_poolpumpe
    state: 'on'
  action:
  - service: switch.turn_off
    entity_id: switch.poolpumpe


- id: poolpumpe_aus_time
  alias: Poolpumpe aus Automation Time
  trigger:
  - platform: time
    at: '08:00:00'
  - platform: time
    at: '13:00:00'
  - platform: time
    at: '19:00:00'
  - platform: time
    at: '22:00:00'
  action:
  - service: switch.turn_off
    entity_id: switch.poolpumpe

I’m glad it is working for you.

I have two things to share:

  1. As I mentioned in my previous post, there’s no need to use three automations. Two automations dedicated to turning off the pump is unnecessarily redundant. If you are concerned about restarts then simply use an automation with explicit off-times and remove the automation that uses the for qualifier. This is not an issue of “better too much than too little”. Using two automations to turn off the pump is “not understanding how the two automations work”.

  2. I’ve noticed a growing trend in this community where a user takes someone else’s idea(s), makes one immaterial change (or even none at all), then re-posts it as their “solution”. Despite the fact they originally didn’t know the answer, asked for help, were given it, then chose to re-package the supplied answer as their own solution. Even worse, sometimes the re-packaged ‘solution’ is incorrect or employs bad practices (the so-called “solution” serves to mislead other users). Team/community-building is not enhanced by re-branding other people’s ideas as your own.

There’s also a way of achieving ‘time trigger 4 times a day’ using a single automation but I’ll leave that for you as an exercise.

i dont why it not working on vacation mode on boolean

my config is

  • id: master_bedroom_lights_on
    alias: Master Bedroom Lights On
    trigger:

    • platform: time
      at: ‘17:15:00’
      condition:
    • condition: state
      entity_id: input_boolean.vacation_mode
      state: ‘on’
      action:
    • service: light.turn_on
      entity_id: light.master_bedroom_cupboard_lights
  • id: master_bedroom_trigger
    alias: Master Bedroom Lights Trigger
    trigger:

    • platform: state
      entity_id: light.master_bedroom_cupboard_lights
      to: ‘on’
      for: ‘02:25:00’
      condition:
    • condition: state
      entity_id: input_boolean.vacation_mode
      state: ‘on’
      action:
    • service: light.turn_off
      entity_id: light.master_bedroom_cupboard_lights
  • id: master_bedroom_lights_off
    alias: Master Bedroom Lights Off
    trigger:

    • platform: time
      at: ‘22:00:00’
      action:
    • service: light.turn_off
      entity_id: light.master_bedroom_cupboard_lights