Can I use a template expression in a Time Interval Trigger for an Automation?

Is it possible to use a template expression inside a time: hours: statement inside a trigger of an automation?

Here’s what I really want to do. This would also mean I could reduce what is currently 8 separate automations (per pump), down to single automation. And since I have 8 pumps to control, that means a difference of 8 Automations vs. 64 Automations.

  • alias: Pump at the specified interval frequency
    trigger:
    • platform: time
      hours: ‘/{{ ( 24 / float(states(“input_select.reefdoser2_pump1_daily_freq”))) | int }}’
      seconds: ‘00’

This is what I have to do. It’s ugly and I for every option I want to offer in the list to choose from, I have to have a separate automation:

  • alias: Pump every 2 hours
    trigger:
    • platform: time
      hours: ‘/2’
      seconds: ‘00’
      condition:
      condition: and
      conditions:
      • condition: template
        value_template: “{{ ( 24 / float(states(‘input_select.reefdoser2_pump1_daily_freq’))) | int == (2) }}”
      • condition: template

Perhaps I’m just doing it wrong, but if it’s not supported, I sure could use it as a new feature.

Advice appreciated :slight_smile:

Let me see if I understand. You have an input_select (per pump) that is effectively how many times a day you want the corresponding automation actions to run. The input_select only provides options that evenly divide into 24. And you want the actions to run on the hour. Is that right?

If so, why can’t you just have one automation that is triggered every hour, and use the condition to see if that hour happens to be one of the hours the input_select specifies? Something like:

- alias: Pump reefdoser2_pump1
  trigger:
    platform: time
    hours: '/1'
    minutes: 0
    seconds: 0
  condition:
    - condition: template
      value_template: "{{ now().hour % states('input_select.reefdoser2_pump1_daily_freq')|int == 0 }}"
    ...
  action:
    ...

Hah…and I wondered if I was being daft. The answer was yes. lol What’s so funny is I now realise I used now().hour in similar expressions elsewhere, but not here. I don’t know what I was thinking now.

Looking forward to trying this out. Think that’ll certainly be more CPU friendly than 64 automations.

Still, I wonder if having templating possibilities in the Interval would even be more efficient? But this will defo work for now. Thanks again! :smiley:

Okay, although I wanted too very badly, I couldn’t really investigate / respond yesterday about the code as my brain was distracted with other pressing issues that I unexpectedly got hit with.

The issue I see with your code suggestion now (thanks again, btw!) is that the operational test perform don’t actually produce the desired effect.

You are right about the input_slider representing the value of Number of times per day the pump should run. Dividing this value against 24 hours in a day, I derive elsewhere that this should represent the interval in “n” hours. So if the user selects 24 times a day, I thought we can evaluate that quickly in a Template Trigger (if I am allowed to do such a thing) with:

trigger:
platform: time
hour: ‘/{{ ( 24 / float(states(“input_select.reefdoser2_pump1_daily_freq”))) | int }}’
seconds: 00

And we’d know what the result is without actually triggering the automation to run and test further conditions (and be more efficient). BTW & just FYI, I don’t define a hard limit to minutes: in the time trigger, because that’s another variable the user can choose to “offset” the pumps so they do not (or do) run at the same synchronised time.

“{{ now().hour % states(‘input_select.reefdoser2_pump1_daily_freq’)|int == 0 }}”

Only when I looked at it again with more focus (and coffee) this morning, did I realise your suggestion wouldn’t work “as is”. The value in the input_select.reefdoser2_pump1_daily_freq object isn’t the actual hour it should run, but the number of times it should run per day.

However, you were on the right track with your suggestion and which kicked me in the butt to realise my own solution which was to divide it into 24, which I’d done elsewhere in my code and BOOM it works. :smiley:

{{ now().hour % ( 24 / float(states(‘input_select.reefdoser2_pump3_daily_freq’)))|int == 0 }}

So now I have the following automation - only ONCE for each pump. YAY!

- alias: Reefdoser1 Pump1 at the specified frequency
  trigger:
    - platform: time
      hours: '/1'
      seconds: '00'
  condition:
    condition: and
    conditions:
      - condition: template
        value_template: "{{ now().hour % ( 24 / float(states('input_select.reefdoser1_pump1_daily_freq')))|int == 0 }}"
      - condition: template
        value_template: "{% if now().minute | int ==  states.input_number.reefdoser1_pump1_daily_mins.state | int %}true{% endif %}"
      - condition: state
        entity_id: sensor.reefdoser1
        state: 'Online'
      - condition: state
        entity_id: input_boolean.r1d1
        state: 'on'
  action:
    - service: switch.turn_on
      entity_id: switch.reefdoser1_pump1
    - delay: '00:00:{{ states.sensor.reefdoser1_pump1_time_split.state | int }}'
    - service: switch.turn_off
      entity_id: switch.reefdoser1_pump1

Thanks again @pnbruckner!

BTW, for anyone who’s curious to know more about my project with dosing pumps, I’ll be releasing the full YAML code on how I did this automatic dosing pump upgrade to Sonoff/MQTT setup very shortly on the Share Your Projects thread.

Yes, it would be nice if you could use a template here. But you can’t. Unless you want to submit a pull request on the code :wink: , it is what it is. (Well, I’m pretty sure anyway!)

Ah, yes. Looks like I needed more coffee when I wrote that part! But in my defense, I had just been helping someone with how to do something “every x hours” (which is what that expression does), so I had that in my brain. Good catch!

Glad I could help and it’s working for you. :smile:

1 Like

I was trying to achieve the same thing, but just for allowing me to control the time period in the UI so that I do not have to change my automation YAML each time I want to make an automation trigger more or less often.

For that I was hoping to be able to use a template expression in a time pattern. By what was discussed here, is it safe to assume that this is not possible?

Well, we were discussing the time trigger, not the time pattern trigger, but yes, neither supports templates.

1 Like