Days selector for automation run

Hi, I would need to create a selector to decide every how many days to run an automation (e.g. every 2 days, every 6 days, etc.), editable with a slider on the lovelace.
Is there any way to create it? Can you advise me? Thanks

Ok but I can’t put it into automation like I would like.
Can you give me an example of an automation that runs every 3 days?

You haven’t told us how you would like to use it in your automation… “Runs every 3 days” is a vague requirement. You need to clarify exactly how you want your automation to work… For example, should it run every x days at a certain time of day? Or, should it run x days after a certain event occurs? If you aren’t specific with your question, it is difficult for us to be specific with an answer.

How to Ask a Good Question

The following automation turns on a switch at 09:00 every X days, where X is the value of input_number.your_interval, and then turns off the switch at 18:00.

alias: Example
trigger:
  - platform: time
    at:
      - '09:00:00'
      - '18:00:00'
condition: "{{ (now() - as_datetime(0)).days % states('input_number.your_interval') | int(1) == 0 }}"
action:
  - service: "switch.turn_{{ iif(now().hour == 9, 'on', 'off') }}"
    target:
      entity_id: switch.your_switch

Like Didgeridrew explained, we don’t know what you want the automation to do other than “run every X days” so the example I provided may require changes to meet your detailed requirements.

I’m sorry, I thought I explained well.
Basically I would like to add to this automation the possibility of deciding every how many days it is performed, for example every 2 days, managed by a slider.

automation: 
- alias: Irrigazione Vasi
  initial_state: true
  trigger:
    platform: template
    value_template: >-
      {{ states('sensor.time') == (states.input_datetime.orario_irrigazione.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}
  condition:
    - condition: state
      entity_id: input_boolean.stato_irrigazione
      state: 'on'
    - condition: template
      value_template: "{{ is_state(state_attr('group.giorni_irrigazione', 'entity_id')[now().isoweekday() - 1], 'on') }}"
  action:
    - service: script.turn_on 
      entity_id: script.irrigazione_vasi
    - wait_for_trigger:
        - platform: state
          entity_id:
            - switch.irrigation_pump
          to: 'on'    
      timeout: 00:00:10
      continue_on_timeout: true

Every 2 days, at what time?

The subsequent example you posted reveals that the scheduled time is controlled by a helper (Input Datetime). You can use a simple Time Trigger for that instead of a lengthy Template Trigger.

automation: 
- alias: Irrigazione Vasi
  initial_state: true
  trigger:
    - platform: time
      at: input_datetime.orario_irrigazione
  condition:
    - condition: state
      entity_id: input_boolean.stato_irrigazione
      state: 'on'
    - condition: template
      value_template: >
        {{ (now() - as_datetime(0)).days % states('input_number.your_interval') | int(1) == 0 }}
  action:
    ... your actions go here ...

The following template determines if the current day is a multiple of 2. Copy-paste it into the Template Editor and experiment with it to see the results it produces.

{{ (now() - as_datetime(0)).days % 2 == 0 }}

Ok thank you, I’ll try to do as you told me and see if the interval works

You marked your own post with the Solution tag. However the example in your post doesn’t meet your requirement for running every 2 days.

Did my example (designed to run every 2 days at the time specified by input_datetime.orario_irrigazione) fail to work? Or did you eliminate the requirement to run every 2 days?

Using the automation editor in HA, I found a template/filter expression that does the trick. Add an IF condition as “Value Template”. Switch to edit in YAML and add the following:
condition: template
value_template: “{{ now().strftime(‘%j’) | int % 2 == 0 }}”

Sorry, I don’t know how to make 2 a variable tied to a slider control.
This example will evaluate to TRUE every other day. Change the 2 to whatever number of days you want. Translated to English: Get the current day of the year and divide by X. Return TRUE if the remainder is 0. Really cryptic but strftime() takes formatting arguments. %j translates to day of year. (j for Julian?)
Other expressions I found were for day of the month which will hickup on the first day of the month depending on days in the month. This one only has a problem on January 1 where you will get more/less days delay due to rolling over from 365 to 001.

The template you posted can fail to work correctly at year end.

It performs its calculation based on the number of days in a year. If a given year has 365 days (an odd number) the value computed on the 365th day will be the same as the one computed on the first day of the next year (also an odd number). It will fail to fulfill the requirement of only running every 2 days.

The template I posted over a year ago doesn’t have this problem because its calculation is based on the number of days since the Unix Epoch.

{{ (now() - as_datetime(0)).days % 2 == 0 }}

FWIW, the author of this topic never responded to my last question; they marked one of their own posts as the Solution even though it fails to meet the requirements they specified.

This illustrates my main beef with HA. There may be 50 different ways to get the result you are looking for but finding even one of them can be a challenge some times.
Today I found there’s a HA Add-On for VSCode. I wonder if that would help here? Definitely going to give it a look this weekend!

Possibly, but in this particular case the template I mentioned can be found three posts above yours.

FWIW, it’s based on a similar request (May 2022):


I don’t believe it offers any support for devising templates. Home Assistant’s Template Editor serves as a sandbox for testing Jinja2 templates and it simply tells you the template’s computed result (or if it contains a Jinja2 syntax error).