Run automation every two days

Hello all,

I am trying to run an automation every two days and i see a lot of old solutions for this but none use the time pattern trigger.

My question is: can I use the time pattern trigger to run the automation every two days at 5 am in the morning for 15 minutes?

When I try to add in the hours section "/48" I get an error that the value should be between 0 and 23. What am I missing here?

Cheers

The time trigger pattern only checks if the current time resembles the value of hour, minute and second and therefore can only be set to from 0-23 for hour.

If you want to set it every 48 hours, you probably have to use some sort of condition template.

You can use garbage collection from hacs.

You can then set us a sensor in configuration.yaml

garbage_collection:
  - name: Boot HACore
    frequency: "every-n-days"
    first_date: '2022-05-12'  
    period: 2

It will be zero when it is the correct day.

my automation looks like this

- id: 'XXXXXXXXX'
  alias: Reboot HA Core
  description: Reboot home assistant at 11:57 PM
  trigger:
  - platform: time
    at: '23:57:00'
  condition:
  - condition: state
    entity_id: sensor.boot_hacore
    state: '0'
  action:
  - service: homeassistant.restart
  mode: single
1 Like

thank you! I will try this. Does it resist reboots other than the ones you do on purpose with this automation?

thank you! I understand it how. I was confused by the documentation and some guides I saw. There is a way apparently to set an automation with the time pattern where you write “/5” for example in the minute section and that means every 5 minutes.
In my mind setting “/48” in the hour section made sense it meant every 48 hours but apparently not.

Oh yeah, I actually didn’t consider you were using the “/”. I think it can only look in the timeframe of 24 hours. But I understand your question better now.

But a trigger of /48, which means every 48 hours doesn’t really makes sense I guess, because it is 48 hours relative of what?

minutes: /5 means on minute 0, 5, 10 etcetera. hours: /2 means on hour 0, 2, 4 …

So always relative to 0 and then added with the /value. I guess they could have set 0, 48, 96 hours. But then you are running something always at 0.00 AM. That makes a lot less sense than every 2 hours during a day.

This automation turns on the switch at 5:00 and off at 5:15 every other day (i.e. every two days) and is able to handle a restart correctly.

alias: Example 1
trigger:
  - platform: time
    at:
      - '05:00:00'
      - '05:15:00'
  - platform: homeassistant
    event: start
condition: "{{ (now() - as_datetime('19700101T00Z')).days % 2 == 0 }}"
action:
  - service: "switch.turn_{{ iif(now().hour == 5 and 0 <= now().minute < 15, 'on', 'off') }}"
    target:
      entity_id: switch.your_device
4 Likes

yes that makes sense. I didn’t consider the relative to what part. I imagine that a trigger based on days could be userful for things like this.

1 Like

For future reference, whenever you restart Home Assistant, or execute Reload Automations, all automations are restarted. That means any automation that was in progress is terminated and re-initialized.

If you use the Automation Editor to compose/modify automations, the moment you click its Save button will cause it to execute Reload Automations.

As for the Time Pattern Trigger, its options are all relative. For example, hours represents hours of the current day (0-23), minutes represents minutes of the current hour (0-59), etc.

Your proposed days option presents a problem because it would be relative to the current year but every year doesn’t have the same number of days.

Yes adding a days option seems useful indeed.

It has also been proposed before: Platform: time_pattern should also support month, day, and day of week

Esphome has a cron option for automations:Time — ESPHome

time:
  
- platform: sntp
    # ...
    on_time:
      # Cron syntax, trigger every 5 minutes
      - cron: '* /5 * * * *'
        then:
          - switch.toggle: my_switch

With cron you can set every 48 hours at time X.

Afaik this is not available in Home Assistant.

It has been proposed: WTH don't we have a cron time trigger

there’s no need as @123 has pointed out with his automation. It’s identical and more flexible than cron.

1 Like

how about an extra parameter to specify the referencce point? for example if you want every 2 days, specify starting from say 1st of June 2022. Then after a restart it will read the starting point and it will know where it is.

The reference point can be the Unix epoch time. That’s what I used in the automation’s template to compute “every second day”.

1 Like

sorry for the silence! I have managed to burn the water valve (my mistake) and now I am waiting for a replacement and I will test out the proposed solutions!

This also works quite well… I don’t use it for everything but it has solved a couple problems for me.

1 Like

thank you for this! I have repaired the valve now and I have implemented your automation. I will see if it works and report back!

I have tried yesterday the sheduler but that didn’t work or I didn’t set it up properly

I may have done something wrong because the automations didn’t start with your implementation. Do you mind looking at the following settings and see what is wrong?

So I have two automations for garden watering:

alias: south lawn sprinklers
description: ''
trigger:
  - at: '07:00:00'
    platform: time
condition: []
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.power_s
  - service: switch.turn_on
    target:
      entity_id: switch.valve_s
    data: {}
  - delay:
      hours: 0
      minutes: 15
      seconds: 0
      milliseconds: 0
  - service: switch.turn_off
    target:
      entity_id: switch.valve_s
    data: {}
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.power_s
mode: single

and

alias: west lawn sprinklers
description: ''
trigger:
  - at: '07:30:00'
    platform: time
condition: []
action:
  - data: {}
    service: switch.turn_on
    target:
      entity_id: switch.valve_1
  - delay:
      hours: 0
      minutes: 15
      seconds: 0
      milliseconds: 0
  - data: {}
    service: switch.turn_off
    target:
      entity_id: switch.valve_1
  - service: switch.turn_on
    target:
      entity_id: switch.valve_2
    data: {}
  - delay:
      hours: 0
      minutes: 15
      seconds: 0
      milliseconds: 0
  - service: switch.turn_off
    target:
      entity_id: switch.valve_2
    data: {}
mode: single

I have turned these off and was hopping to turn the automations on with your automation like this:

alias: garden irigation
description: starting garden irigation every two days
trigger:
  - platform: time
    at:
      - '05:00:00'
      - '09:00:00'
  - platform: homeassistant
    event: start
condition:
  - condition: template
    value_template: '{{ (now() - as_datetime(''19700101T00Z'')).days % 2 == 0 }}'
action:
  - service: automation.turn_{{ if(5 < now().hour < 9, 'on', 'off') }}
    target:
      entity_id:
        - automation.west_lawn_sprinklers
        - automation.south_lawn_sprinklers

Your automation ran today but it didn’t turn on the other two automations to actually trigger the watering!

This needs to be a double “i” => iif => immediate if

See here:

I would never suggest to do what you have done (i.e have one automation turn on/off other automations). My posted example turns a switch on/off.

There’s no reason to turn on/off the two automations. Just add the Template Condition, that I had suggested two months ago, to each automation so that it will execute its actions every two days.

(In addition, as paddy0174 already mentioned, your example misspells iif)

ok I am changing things and following your advice. I have made a small example with your automation but I get an error when I test the condition. I have copied and pasted your automation and when I switch to the visual editor in the condition section I get the test button. When I press it I get the following error:

template value should be a string for dictionary value @ data['value_template']. Got None

Here is the example:

alias: Example 1
trigger:
  - platform: time
    at:
      - '07:00:00'
      - '07:15:00'
  - platform: homeassistant
    event: start
condition:
  - condition: template
    value_template: '{{ (now() - as_datetime(''19700101T00Z'')).days % 2 == 0 }}'
action:
  - service: >-
      switch.turn_{{ iif(now().hour == 7 and 0 <= now().minute < 15, 'on',
      'off') }}
    target:
      entity_id: switch.valve_1

You originally wrote the condition like this:

condition: "{{ (now() - as_datetime('19700101T00Z')).days % 2 == 0 }}"

I copy and paste your entire automation in a new automation using the yaml editing method (I cannot create it using the UI editor ) and I press save and it changes to what I have and I get the error I mentioned.
What should I do?