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

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
1 Like

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