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

Hi guys!
I’m trying to do automation for a watering system for my bonsai.
I made a system that only depends on a switch.

So I want this automation to turn on that switch every 2 days, but also have the possibility to increase or decrease this frequency. I thought that a counter would help but I couldn’t figure how.

Here’s what I have so far:

- id: '1594200781640'
  alias: Rega - ligar
  description: ''
  trigger:
  - hours: /48
    platform: time_pattern
  condition: []
  action:
  - data: {}
    entity_id: switch.rega
    service: switch.turn_on

I’m not sure that this will work, I was hoping that someone could help me with an idea for this kind of automation.

Any solutions or suggestions are welcome!

Hi, I think input number should do for the variable part…

But, i don’t think that - hours: /48 would work
What I would do is use a template trigger

1 Like

Thanks for the reply!

The input number should help me with the increase/decrease.
About the templates, those things scare me :smiley: I honestly don’t know how to start with those.
But I’ll take a look!

1 Like

As far as I know, the Time Pattern Trigger does not allow you to template the time.

You said you want to turn on the switch every 2 days but at what time of the day? Do you want to be able to adjust the time via the UI as well?

The time of the day can be static, for instance at 11:00 AM.
I’m struggling a bit on this.

I’m thinking of making some sort of schedule table with buttons for every day of the week, but for this I need 7 automations, each button will toogle the automation of the corresponding weekday.

Not the most elegant solution, but I can’t find another solution.

How about using this?

1 Like

The following alarm clock function can’t do “every X days”; you must specify which days of the week.

Can the one you suggested do “every X days”? I was thinking about how to create that feature but would happily just borrow an existing one.

3 Likes

Maybe adding sensor time_date
and

automation:
  trigger:
    platform: template
    value_template: "{{ ((states.sensor.time.last_changed - states.YOUR.ENTITY.last_changed).total_seconds() / 3600) >  states('input_number.hours') }}"

This should trigger every tot hours you set via input_number

:wave:t2:

1 Like

Thank you guys for the help!
There are here some wicked solutions! Great job!

@123 I think your alarm clock solution might be the one! It seems simple and does exactly what I want!

I will try it and post the result later!

That alarm clock he made is crazy nice.
It should be implemented in stock HA as an alarm clock.

Kind of like you add an input alarm_clock in configuration and you get all that done in the background.

I don’t have the need for one now but if I had…

1 Like

As promised and after a few adjustments, here’s the final result:

Thank you guys for taking the time to help me!
The result is way better than I imagined.

And I totally agree with @Hellis81 this alarm clock should be featured in HA!

1 Like

Hello,
I have a similar problem.
I want the trigger to sheck if the automatisation last run have bin in less day’s that the input_number.
I try this but don’t work

- id: '1595533899655'
  alias: 3 - ARROSSAGE auto
  description: ''
  trigger:
  - entity_id: sun.sun
    platform: state
    to: sunset
  condition:
  - condition: template
    value_template: '{(now().strftime("%s")-states('automatisation_3 - ARROSSAGE auto').last_changed.strftime("%s"))/(3600*24) )>(state('input_number.intervalle_arrosage')}'

There are many things wrong in this template:

    value_template: '{(now().strftime("%s")-states('automatisation_3 - ARROSSAGE auto').last_changed.strftime("%s"))/(3600*24) )>(state('input_number.intervalle_arrosage')}'
  • The template should use double parentheses {{ }} not single parentheses { }.
  • The states() function requires a valid entity_id. This is not a valid entity_id: “automatisation_3 - ARROSSAGE auto”
  • The states() function produces a string value which does not have a method called last_changed. The means you cannot do this: states('automatisation_3 - ARROSSAGE auto').last_changed
  • The output of the states() function is always a string value. That means you cannot perform a numerical comparison using a string value. You must convert the string to a number using either the int or float filter.
  • You cannot mix the use of single (') and double (") quotes within the template. Use one type of quote at the beginning and end of the template and another type of quote inside the template. For example: "{{states('light.cuisine')}}"
  • The last function in the template is state which does not exist. You probably meant to use states.

Stumbled upon this while looking on how to make a 2 over 2 days automation. Basically like we split some chores with my wife like that, 2 days me 2 days her. Wanted to make it so Hass dashboard shows who’s shift it is.

Ended up using a slightly modified version of something from this thread. Here’s how it looks in the editor:

Here’s the template:

(states.sensor.time.last_changed.date() - states.input_boolean.every_2_days.last_changed.date()).days >= 2

So basically it toggles a boolean every 2 days. I thought I’d need to use choice condition to do like “if its on turn it off” before I rememberd there’s a “toggle” action :smiley: So it makes it very simple.

The main difference is that this is based on days, it does not take time into account. However it is also possible to adjust this to add time offsets, to make it change on specific hour of a day, if needed. This, hwoever, will change at 00:00 as the new day comes.

EDIT:
It also looks like it’s better to use now().date() instead of time sensor, as for me it’s last updated did not change for hours for some reason… Maybe it’s by design, not sure.

(now().date() - states.input_boolean.every_2_days.last_changed.date()).days >= 2

Hi, just saw this thread - exactly what I am looking for.
Wanted to add it into my boolean yaml, but it looks I missed something. As the sensor does not have an initial last_changed info, it does never update. Does anybody has any help on this?

Thx

- platform: template
  sensors:
    testevery2days:
      value_template: >-      
        {{ if (now().date() - states.sensor.testevery2days.last_changed.date().days', '>1')  }}
          {% set testevery2days = 'on' %}
        {% else %}
          {% set testevery2days = 'off' %}
        {% endif %} 

Your template’s first line is incorrect but that’s overshadowed by the fact it doesn’t return a result.

Try this version:

- platform: template
  sensors:
    testevery2days:
      value_template: >-
        {{ 'on' if (now().date() - states.sensor.testevery2days.last_changed.date()).days > 1 else 'off' }}

NOTE

I assume you know these two will produce different results:

 (now().date() - states.sensor.testevery2days.last_changed.date()).days

versus:

 (now() - states.sensor.testevery2days.last_changed).days

The first one rounds up. For example, if the time difference is 1 day and 20 hours, it will report 2 days. The second one doesn’t do any rounding and will report 1 day.

Great - thank you so much - I will test both versions!

Just realized, that it looks that the template needs an initial value as the “last_changed” needs at least one change to start. Thought I could manage this with a automation after startup, but could not use this value_template sensor in the automation.
See below error message:

TemplateError('UndefinedError: 'None' has no attribute 'last_changed'') while processing template 'Template("{{ 'on' if (now().date() - states.sensor.test_every2days.last_changed).days > 1 else 'off' }}")' for attribute '_state' in entity 'binary_sensor.test_every2days'

Just add an initial test to the template to ensure the value of last_changed is not none. That’ll be sufficient to get over the initial hurdle.

OK I tried it, error in log is gone but the boolean is still unavailable if I test it afterwards, shouldn’t it be set right now to on and therefor also a last change ? Following my template sensor:

- platform: template
  sensors:
    test_every2days:
      value_template: >-
        {{ 'on' if  states('sensor.test_every2days') == 'unavailable'}}
        {{ 'on' if (now().date() - states.sensor.test_every2days.last_changed).days > 1 else 'off' }}