Run automation every two days

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

Yes, a Time Trigger accepts the use of an Input Datetime.

That looks neat! How can I adapt it to my sprinkler system when I use ‘input number’ as a way to turn them on?

service: input_number.set_value
data:
  value: 20
target:
  entity_id: input_number.gardenbedtimer

But then I’d also like for the automation to know if I had previously manually started the sprinkler and count 3 days after that?

What part of what you requested is related to running an automation every other day?

@123 Hi Im trying to replicate your example but with a little bit more complexity. I want the automation to work every other day but this every two days I want it to run at two different hours 8:45 am and 2:00 pm.
I just want the espresso machine to turn on and I add a timer that send a notification when the machine is heated. This is what I come up based on your example. Can you let me know if it looks good?

alias: espresso_time
description: ""
trigger:
  - platform: time
    at: "08:45:00"
    id: morning_coffee
  - platform: time
    at: "14:00:00"
    id: afternoon_coffee
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.coffee_heated
    id: coffee-heated
condition:
  - condition: and
    conditions:
      - condition: state
        entity_id: person.luis_rivera
        state: home
      - condition: template
        value_template: "{{ (now() - as_datetime('19700101T00Z')).days % 2 == 0 }}"
action:
  - choose:
      - conditions:
          - condition: trigger
            id: morning_coffee
        sequence:
          - service: timer.start
            data: {}
            target:
              entity_id: timer.coffee_heated
          - service: "switch.turn_{{ iif(now().hour == 8 and 45 <= now().minute, 'on') }}"
            target:
              entity_id: switch.maquina_espresso
      - conditions:
          - condition: trigger
            id: afternoon_coffee
        sequence:
          - service: timer.start
            data: {}
            target:
              entity_id: timer.coffee_heated
          - service: "switch.turn_{{ iif(now().hour == 14 and 0 <= now().minute, 'on') }}"
            target:
              entity_id: switch.maquina_espresso
      - conditions:
          - condition: trigger
            id: coffee-heated
        sequence:
          - service: notify.pushover
            data:
              message: Hora de hacer un buen cafe!
              title: Espresso
mode: single      

You don’t need to differentiate or set up Choose options for triggers that have the same desired actions. The same goes for using an iif() function.

alias: espresso_time
trigger:
  - platform: time
    at: 
      - "08:45:00"
      - "14:00:00"
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.coffee_heated
    id: coffee-heated
condition: 
  - "{{ (now() - as_datetime(0)).days % 2 == 0 }}"
  - condition: state
    entity_id: person.luis_rivera
    state: home
action:
  - choose:
      - conditions:
          - condition: trigger
            id: coffee-heated
        sequence:
          - service: notify.pushover
            data:
              message: Hora de hacer un buen cafe!
              title: Espresso
    default:
      - service: timer.start
        data: {}
        target:
          entity_id: timer.coffee_heated
      - service: switch.turn_on
        target:
          entity_id: switch.maquina_espresso
mode: single

Thanks for the quick response but Im getting this error Message malformed: Unexpected value for condition: '{{ (now() - as_datetime(0)).days % 2 == 0 }}'. Expected and, device, not, numeric_state, or, state, sun, template, time, trigger, zone @ data['condition'][0]

I neglected to erase the extraneous condition: that was in the original, which is not used for the shorthand method. I have corrected it above.

FWIW, you could get a little more functionality out of the automation by reorganizing it a little more… this way the time triggers only work on your every-other-day schedule, but if you manually turn on the switch to warm up the machine you will get your notification no matter what day it is…

Reorganized Automation
alias: espresso_time
trigger:
  - platform: time
    at: 
      - "08:45:00"
      - "14:00:00"
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.coffee_heated
    id: coffee-heated
  - platform: state
    to: 'on'
    from: 'off'
    entity_id: switch.maquina_espresso
    id: switch
condition:
  - condition: state
    entity_id: person.luis_rivera
    state: home
action:
  - choose:
      - conditions:
          - condition: trigger
            id: switch
        sequence:
          - service: timer.start
            data: {}
            target:
              entity_id: timer.coffee_heated
      - conditions:
          - condition: trigger
            id: coffee-heated
        sequence:
          - service: notify.pushover
            data:
              message: Hora de hacer un buen cafe!
              title: Espresso
    default:
      - condition: "{{ (now() - as_datetime(0)).days % 2 == 0 }}"
      - service: switch.turn_on
        target:
          entity_id: switch.maquina_espresso
mode: parallel

Worked great. Thank you very much!

Hi I realized today that I write that I wanted to turn on every other day with works great but what I really want is to turn on every two days. what I need to change? Thanks for the reorganized automation but the way im turning on the machine is kind of a hack so I can turn it on with home assistant but I have no way to know the state of the machine in HA if I turn it on manually.

% 3

I change the automation @Didgeridrew suggest to take advantage of the notification if i turn on the machine manually but Im getting this error Message malformed: expected dictionary @ data[‘action’][0][‘default’][0]

alias: espresso_time
trigger:
  - platform: time
    at:
      - "08:45:00"
      - "14:00:00"
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.coffee_heated
    id: coffee-heated
  - platform: state
    to: "on"
    from: "off"
    entity_id: switch.maquina_espresso
    id: switch
condition:
  - condition: state
    entity_id: person.luis_rivera
    state: home
action:
  - choose:
      - conditions:
          - condition: trigger
            id: switch
        sequence:
          - service: timer.start
            data:
              duration: 0
            target:
              entity_id: timer.coffee_heated
      - conditions:
          - condition: trigger
            id: coffee-heated
        sequence:
          - service: notify.pushover
            data:
              message: Hora de hacer un buen cafe!
              title: Espresso
    default:
      - "{{ (now() - as_datetime(0)).days % 3 == 0 }}"
      - service: switch.turn_on
        target:
          entity_id: switch.maquina_espresso
mode: parallel