Run automation every two 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?

This error comes from not using quotes and double quotes correctly…

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

You need to escape this correctly:

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

Here is what happens when I use what you posted:

I pasted what you wrote in the value template field.

This is a bug in the template editor. Try testing it in a “real world” example.

I am sorry but I don’t understand. What do you mean by a “real world” example? You mean to create the automation in command line?

Save it as is, and let it run, it will work. The testing is, what’s not working.

If you want to take a look, use the Developer Tools and paste that piece of code into the template editor.

ok cool! I made a test automation to switch the water on every two days and now I am waiting! will report back

1 Like

That bug has misled so many people that it deserves some kind of ‘most evil’ award. :smiling_imp:

I’ve lost count of the number of times someone reports that a perfectly good template is “bad” because it fails the test in the Automation Editor.

1 Like

Maybe one (you?) should make a thread about that, so we can easily link to this as an explanation? Maybe something that could get pinned on top? (@petro ?)

Nonetheless it’s annoying, because people loose their faith. :slight_smile: Even I had that moment a few days ago, it took me a few seconds before realizing it’s the bug - and not me! :rofl: :rofl:

I works! It turned on today when it was supposed to! Tomorrow the condition will be false and it will not turn on. I will create a few helpers to set the start times and number of days easier. I assume this is possible no?

In any case thank you!

1 Like