Trigger time_pattern with input_number

Hi all,
i have this automation trigger:

 trigger:
   - platform: time_pattern
     minutes: /15

I want if is possible to change the minutes with an input number… is possible? if yes how?

Also i have this action:

action:
    - delay: "00:{{ '{:02}'.format(range(0, 13) | random | int) }}:00"

and i want to be able to use two different input_number to change the 0 and the 13 that are the range of the random … anyone have some suggestion?
Thanks

1 Like

The time_pattern trigger doesn’t accept a template, but you could do it with a template trigger if you include sensor.time (or even sensor.date_time) in your configuration.

trigger:
  platform: template
  value_template: >
    {{ states('sensor.time')[-2:]|int %
       states('input_number.xyz')|int == 0 }}

This takes the last two digits of sensor.time's state (which is minutes) and converts it to an integer, then takes the state of the input_number and converts it to an integer, then does modulo math to determine if the minute of the current time is a multiple of the input_number.

For the random delay, first you can simplify what you have to:

- delay: "{{ range(0, 13) | random | multiply(60) | int }}"

or:

- delay:
    minutes: "{{ range(0, 13) | random | int }}"

To add in the input_number's:

- delay:
    minutes: >
      {% set mn = states('input_number.minimum')|int %}
      {% set mx = states('input_number.maximum')|int %}
      {{ range(mn, mx+1) | random | int }}
1 Like

i don’t know why but the trigger is not working

Have you made any progress? If not, what do you get if you put the following in the template editor:

{{ states('sensor.time') }}
{{ states('input_number.xyz') }}
{{ states('sensor.time')[-2:]|int %
   states('input_number.xyz')|int == 0 }}

Of course change input_number.xyz to the entity_id of your input number entity.

Try to get it to be re-evaluated as the time changes (e.g., put the cursor at the end and type a space, then backspace, then space, then backspace, … – each time you type and then wait the template should be re-evaluated.) Do you see the last template changing from False to True?

I hadn’t actually tried my suggestion, but I just did. In order for it to work the input_number must be at least 2. The reason is that after the first time the template trigger fires, it won’t fire again unless the template first renders False and then back to True. Because sensor.time only updates once a minute, if the input_number is set to 1, the template will always render True, and hence cannot trigger more than once.

Here’s an alternative idea (that doesn’t require sensor.time):

- trigger:
  - platform: time_pattern
    minutes: /1
  condition:
  - condition: template
    value_template: >
      {{ now().minute % states('input_number.xyz')|int == 0 }}
  action:
  ...

I tried it and it seems to work.

3 Likes

I needed this too but with seconds, so I leave it here for future reference.

- trigger:
  - platform: time_pattern
    seconds: /1
condition:
- condition: template
  value_template: >
     {{ now().second % states('input_number.xyz')|int == 0 }}
action:
...

This works great (and without sensor.time as previously suggested).Thanks @pnbruckner

edit: added modified condition

2 Likes

But with a trigger of /1 minutes isn’t a condition with seconds rather superfluous ?

Yeah, I forgot to say that I obviously changed the trigger to seconds: '/1'
I’ve edited the previous post

Referring to Help with waiting_for_trigger time pattern template - #7 by 123 the automation is only executing every minute an not every second

I have a wait for template trigger but it only fires every full minute

        - variables:
            var_localNow: "{{ now() }}"
        - wait_for_trigger:
            - platform: template
              value_template: "{{ (now().fromisoformat(var_localNow) + timedelta(seconds=var_ReminderTimerSeconds)) <= now() }}"

anyone have an idea how to force this to start every var_ReminderTimerSeconds seconds?

Hello. This configuration is currently not working. Could you update?

This is a really old topic. I’d suggest starting a new topic specific to your situation. Feel free to tag me. But do include your current YAML code, and explain what you’re trying to do, and why you think it’s not working the way you want.

trigger:
  - platform: time_pattern
    minutes: /1
condition:
  - condition: template
    value_template: >-
      {{(now().minute + (now().hour * 60))  %
      states('input_number.hydroponics_water_on_frequency') | int == 0 }}