How to tigger automation every 2 days and have the possibility to change the frequency?

That’s creative what you did there but not what I had suggested. Unfortunately I don’t have time at the moment to sort this out for you. Hopefully someone else can help you; if not, I will take another look this evening.

Oh that would be great, I am really stucked. It looks that simple only one if/else but…
it looks I missed some important details. Thx for looking into this once again. Cheers

Try this. No guarantee it will work but it doesn’t seem to produce error or warning messages.

      test_every2days:
        value_template: >-
          {% if states.sensor.test_every2days.last_changed is defined %}
            {{ 'on' if (now().date() - states.sensor.test_every2days.last_changed.date()).days > 1 else 'off' }}
          {% else %}
            {{ 'off' }}
          {% endif %}

If it fails to work (or produces errors) … :man_shrugging:

Thanks a lot for your help - looks it does not work, the sensor is still undefined.
I managed it right now with an input_boolean helper which I could set in an automation.
cheers

That’s weird because I currently have that sensor and here’s what it looks like:

Screenshot from 2021-03-30 15-06-28

That’s really weird, I look at the top of this side, there the sensor is still mentioned as “unavailable” but looking into the table you provided, it looks as off (also in my system). So, I will double check and let it run for a couple of days. Thank you!

FWIW, my instance of the sensor just changed state today (2 days after it was created) to on (at 00:00:00).

However, a minute later, it set itself back to off. I don’t know if that’s how you want it to work.

yeah, I have seen today, that the sensor is still off, so most probably it was on at 00:00:00 and then switched back to off. I am right now solving this with an automation and a input_boolean sensor.

Here’s a completely different approach. Say you want something to happen at 08:00 but only every other day (every 2 days).

alias: example two days
trigger:
- platform: time
  at: '08:00:00'
condition:
- condition: template
  value_template: "{{ now().timetuple().tm_yday % 2 == 0 }}"
action:
- service: notify.persistent_notification
  data:
    title: 'Every 2 days.'
    message: "{{ now().timestamp()|timestamp_local() }}"

If you want every 3 days, change the 2 in the condition to 3 (or whatever interval you want).


UPDATE

There is a flaw in this technique that can occur during the transition from the end of one year to the next. For example, if both years have an odd number of days, the automation will trigger on the last day of the current year and on the first day of the next year. That’s two consecutive days as opposed to every other day. Depending on the application, that one instance of failing to skip a day might be unacceptable.

A superior way to calculate ‘every other day’ was presented by Troon in this post.

Here is a revised version of the suggested automation employing the better way to compute ‘skipping a day’.

alias: example two days
trigger:
- platform: time
  at: '08:00:00'
condition:
- condition: template
  value_template: '{{ (now().timestamp() / 86400) | int % 2 == 0 }}'
action:
- service: notify.persistent_notification
  data:
    title: 'Every 2 days.'
    message: "{{ now().timestamp()|timestamp_local() }}"
4 Likes

Lean and smart solution. Super!