Time switch/clock timer automation to toggle entity on/off in defined time frames

Personally, the more I think about it, I’d use 4 automations:

  1. Pump #1 - ON
    Trigger #1 - Time - 6am
    Trigger #2 - Time - 11am
    Trigger #3 - Time 6pm
    Action - Turn Pump #1 On

  2. Pump #2 - ON
    Trigger #1 - Time - 6am
    Trigger #2 - Time - 11am
    Trigger #3 - Time 6pm
    Action - Turn Pump #2 On

  3. Pump #1 - OFF
    Trigger #1 - Time - 8am
    Trigger #2 - Time - 1pm
    Trigger #3 - Time 8pm
    Action - Turn Pump #1 Off

  4. Pump #2 - OFF
    Trigger #1 - Time - 8am
    Trigger #2 - Time - 1pm
    Trigger #3 - Time 8pm
    Action - Turn Pump #2 Off

I realize it’s more than you ‘want’, but it gives granularity to easily change / update on / off / run times for each pump - it also avoids long DELAYs which won’t survive a restart.

Again, HA is a platform that gives you TONS of flexibility and LEGO PIECES to allow you to build what you want - most of the time the same results can be accomplished in dozens of a ways - really a lot of personal preference - and I’ve definitely found that often the way w/ the least automations/scripts/pieces isn’t the easiest to update / maintain. Anyhow, just my $.02

Mark, I agree and argued this myself.
Ultimately, the OP doesn’t see it that way.
He needs time to consider and the experience of maintaining before he’ll come round.
Let’s give him exactly what he’s asked for and let him draw his own conclusions
I reckon, about 6 months as he’s pretty much there and asking the right questions

Dinner calling I’ll be afk for awhile

Thanks, that’s also what I came up with in the meantime but I still try to bring it down to just one automation per pump. I was thinking about probably writing a custom component for a time schedule and then use a state trigger using this schedule along with a service_template as action for on/off. Or I end up using AppDaemon since I can just write one logic there and pass some variables for entities and time spans which makes it pretty reusable without having to duplicate the logic like with a package approach.

Thanks for your help and all the suggestions guys - I might go with the 4 automations for now and try to improve those on the long run.

okay, here is the package, split as required : -

input_datetime:
  id_switch_pump1_tmeslt1_on:
    name: Time Slot 1 On Time
    has_time: true
    icon: mdi:clock-start
  id_switch_pump1_tmeslt1_off:
    name: Time Slot 1 Off Time
    has_time: true
    icon: mdi:clock-end
  id_switch_pump1_tmeslt2_on:
    name: Time Slot 2 On Time
    has_time: true
    icon: mdi:clock-start
  id_switch_pump1_tmeslt2_off:
    name: Time Slot 2 Off Time
    has_time: true
    icon: mdi:clock-end
  id_switch_pump1_tmeslt3_on:
    name: Time Slot 3 On Time
    has_time: true
    icon: mdi:clock-start
  id_switch_pump1_tmeslt3_off:
    name: Time Slot 3 Off Time
    has_time: true
    icon: mdi:clock-end
    
automation:
    #name: Switch Pump01
  - alias: au_switch_pump1
    trigger:
      - platform: template
        value_template: "{{ states('sensor.time') == states('input_datetime.id_switch_pump1_tmeslt1_on') [0:5] }}"
      - platform: template
        value_template: "{{ states('sensor.time') == states('input_datetime.id_switch_pump1_tmeslt1_off') [0:5] }}"
      - platform: template
        value_template: "{{ states('sensor.time') == states('input_datetime.id_switch_pump1_tmeslt2_on') [0:5] }}"
      - platform: template
        value_template: "{{ states('sensor.time') == states('input_datetime.id_switch_pump1_tmeslt2_off') [0:5] }}"
      - platform: template
        value_template: "{{ states('sensor.time') == states('input_datetime.id_switch_pump1_tmeslt3_on') [0:5] }}"
      - platform: template
        value_template: "{{ states('sensor.time') == states('input_datetime.id_switch_pump1_tmeslt3_off') [0:5] }}"
    action:
      - service_template: >
          {% set t = states('sensor.time') %}
          {% set slt1 = states('input_datetime.id_switch_pump1_tmeslt1_on') [0:5] %}
          {% set slt2 = states('input_datetime.id_switch_pump1_tmeslt2_on') [0:5] %}
          {% set slt3 = states('input_datetime.id_switch_pump1_tmeslt3_on') [0:5] %}
          {% set pumpon = (t == slt1) or (t == slt2) or (t == slt3) %}
          {{ 'switch.turn_on' if pumpon else 'switch.turn_off' }}
        entity_id: switch.switch_pump01

It’s “one” automation.
NOTE : this does not protect the user from having the off before the on or overlapping times.
This is not idiot proof !

You should also see how easy it would be to do a search and replace (after you’ve copied the file) from pump1 to pump2 and Pump01 to Pump02

I could also streamline the template but this way, it’s easy to understand and change

It passes config check but I couldn’t test it in anger, edit: there was an error (now corrected)

1 Like

Hi @tom_l
Sorry but i’m a newby in home assistant…
Could you give me your code thanks to create your two boolean buttons please :

  • AM On Time
  • Enable AM Schedule
    Could you change hour on AM On Time directly on your card ? If no, do you know if its possible ?
    Thanks in advance for your help !

This isn’t a button or an input boolean, it is an input datetime.

This is simply the automation enable toggle. Put an automation in an entities card and this is what it looks like.

Yes input datetimes are editable in the card.

Thanks a lot @tom_l
I will try it !

Hi @tom_l
I’ve done this :

input_datetime:
only_time:
name: Réglage heure
has_date: false
has_time: true

It works, do you know how can I make time as a dropdown choice in my card ?

And after, I want to create an actonable action on ios in order ask me each day at 8:00pm to modify this time ? The goal is to switch on my heater 20 minutes before awaking. Do you know how to do that ?

Regards