Automation with time_pattern using state of an input_number

Hi all

I’m trying to create an automation using time_pattern
I would like to use the state of an input_number , but I get error

Is it possible and how can I fix it?

description: test avviso temp basse
mode: single
trigger:
  - platform: time_pattern
    minutes: "/{{ states('input_number.frequenza_avviso_temp_bassa') | int }}"
condition:
  - condition: time
    after: input_datetime.ora_inizio_avviso_temp_bassa
    before: input_datetime.ora_fine_avviso_temp_bassa
    weekday:
      - sun
      - sat
      - fri
      - thu
      - wed
      - tue
      - mon
action: []
alias: test avviso temp basse

Thanks

Time pattern trigger is not templatable. For the same effect:

trigger:
  - platform: template
    value_template: "{{ now().minute % states('input_number.frequenza_avviso_temp_bassa')|int(1) == 0 }}"
1 Like

Thanks

What I would like to achieve is to have an automation run every x minutes between y and z where x is the number of minutes defined in an input_number, y and z is the time range defined in two input_date_time
All these three entities are defined in a lovelace card.

Do you think the automation in first post can do this?

Thanks

If you use my trigger in your automation, that will work as expected provided the interval is an integer factor of 60.

Alternatively:

trigger:
  - platform: time
    at: input_datetime.ora_inizio_avviso_temp_bassa
  - platform: state
    entity_id: automation.ENTITY_ID
    attribute: last_triggered
    for:
      minutes: "{{ states('input_number.frequenza_avviso_temp_bassa') | int }}"

Rest of the automation as before.

This will trigger at the start time, and whenever it’s been x minutes since the last trigger.

Not sure what will happen if you restart HA or reload automations whilst it’s running.

1 Like

Ok, thanks.

This will let the automation repeat every x minutes within the time defined in the condition and is triggered by the start time and the template sensor ?

Good point that I have to check what happens if HA is restarted

Thanks again

1 Like

I was wrong about not being able to trigger off attribute changes with for: — you can (docs, and I ran my own test to check), and therefore don’t need the template sensor.

Edited above.

Correction, as long as the result is a minutely result. now() only updates on the minute inside templates, nothing else. Any more granularity and you’d need to use a time_pattern trigger with a condition.

Whilst I get the minute update frequency, not sure I understand your point about needing correction. My trigger was:

trigger:
  - platform: template
    value_template: "{{ now().minute % states('input_number.frequenza_avviso_temp_bassa')|int(1) == 0 }}"

So long as the integer conversion of the input_number doesn’t result in 0, I think this should always behave the same way as time_pattern? 27 would fire at x:00, x:27 and x:54; 69 would only fire at x:00.

Provided the interval is an integer factor of 60 (as I stated), it should even work as expected: 20 would fire at x:00, x:20 and x:40.

Am I missing your point?

What is the range of x?

Is it 1 to 60? Or does it exceed 60?

1 Like

I have not chosen right now, it can be also more that 1 hour.
What is the difference?

Thanks

time_pattern and my template trigger won’t work with values greater than 60: they will only trigger on the hour. They work by seeing if the clock minute value divided by the frequency number (strictly, a period not a frequency) is an exact integer. That’s why they only work as expected if the number is an integer factor of 60 (1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60).

My trigger in post 4 will work with any value from 1 upwards, including values larger than 60.

Ok, I’ve got it

Thanks

1 Like

I’m understanding is that my automation is more complicated than I think

My needs are
Within the give time in condition, if a temperature changes and is below a given one, the automation have to run and do what in it.
Probably I need also a boolean to set to on when the automation is already runnig to avoid to start again if the temperature changes but it’s still below the given one.

I can understand when to set the boolean to on, but how to set to off if the temperature goes above the given one?

This is my new version

alias: test avviso
description: test avviso
trigger:
  - platform: template
    value_template: >-
      {{ now().minute %
      states('input_number.frequenza_avviso_temp_bassa')|int(1) == 0 }}
condition:
  - condition: time
    after: input_datetime.ora_inizio_avviso_temp_bassa
    before: input_datetime.ora_fine_avviso_temp_bassa
    weekday:
      - sun
      - sat
      - fri
      - thu
      - wed
      - tue
      - mon
  - condition: numeric_state
    entity_id: sensor.temperatura_netatmo
    below: input_number.temperatura_minima_avvisi
action:
  - service: light.toggle
    metadata: {}
    data: {}
    target:
      entity_id: light.studio
  - service: input_boolean.turn_on
    metadata: {}
    data: {}
    target:
      entity_id: input_boolean.avviso_temp
mode: single

actually I’m doing a toggle of a light to understand if the automation is running right

Thanks

OK, so:

alias: test avviso
description: test avviso
trigger:
  - platform: numeric_state
    entity_id: sensor.temperatura_netatmo
    below: input_number.temperatura_minima_avvisi
  - platform: time
    at: input_datetime.ora_inizio_avviso_temp_bassa
  - platform: state
    entity_id: automation.test_avviso
    attribute: last_triggered
    for:
      minutes: "{{ states('input_number.frequenza_avviso_temp_bassa') | int }}"
condition:
  - condition: time
    after: input_datetime.ora_inizio_avviso_temp_bassa
    before: input_datetime.ora_fine_avviso_temp_bassa
  - condition: numeric_state
    entity_id: sensor.temperatura_netatmo
    below: input_number.temperatura_minima_avvisi
action:
  - service: light.toggle
    metadata: {}
    data: {}
    target:
      entity_id: light.studio
mode: single

Triggers at your start time (in case it’s already cold at that time), whenever the temperature drops below the threshold value, and x minutes since the last run; the conditions check that you’re both within your time range and cold.

Note that the badly-named last_triggered attribute only updates when the action section is reached.

1 Like

Thanks, it seems more clear now with your changes

Note that this could run twice in quick succession if the temperature drops very soon after the start time. You could add an additional condition to check how long it’s been since the last run if that matters to you.

In shorthand template notation:

- "{{ now()|as_timestamp-state_attr('automation.test_avviso','last_triggered')|as_timestamp(0) >= states('input_number.frequenza_avviso_temp_bassa')|int(0) * 60 }}"
1 Like

You mean like this?

  - condition: template
    value_template: >
      "{{
      now()|as_timestamp-state_attr('automation.test_avviso','last_triggered')|as_timestamp(0)
      >= states('input_number.frequenza_avviso_temp_bassa')|int(0) * 60 }}"

60 stand for seconds?

Thanks

Yes, that’s right although remove the surrounding quotes; and assuming all the entity IDs are correct. That checks if:

the current time as a UNIX timestamp (seconds since 1970-01-01)
minus
the last run of the automation also as a UNIX timestamp
is greater than or equal to
your input_number (in minutes) times 60 (to match the seconds)

1 Like

Sorry, thought you were talking about % 60 with seconds when the template only executes on the 0th second.

1 Like