Value template trigger - 'for' syntax

I’m trying to setup an automation using a value template trigger:

{% if is_state('climate.bedroom_ac', ['heat', 'fan_only', 'cool', 'dry', 'heat_cool'])%}true{% endif %}
for: 00:15:00

"If my Bedroom A/C is in any of these listed states, for X period of time, evaluate as True"

I can get the value template to work without for, but my desired automation would require it.

I’m using almost the exact same syntax from the documentation for template triggers.

Here is the example code copied over:

automation:
  trigger:
    - platform: template
      value_template: "{% if is_state('device_tracker.paulus', 'home') %}true{% endif %}"

      # If given, will trigger when template remains true for X time.
      for: "00:15:00"

I have tried using these different for formats:

for:
  hours: 0
  minutes: 15
  seconds: 0
for: "00:15:00"
for:
  minutes: 15

I’ve gotten none of them to work.

I suspect the for needs to be somewhere inside the if block, but its shown that way in the docs and I’ve gotten errors when testing it out in the developer tools.

It’s not the for it’s your template (it’s invalid). EDIT No, it’s valid; I was unaware of a recent enhancement; see CentralCommand’s post below.

alias: example
trigger:
  - platform: template 
    value_template: "{{ states('climate.bedroom_ac') in ['heat', 'fan_only', 'cool', 'dry', 'heat_cool'] }}"
    for: '00:15:00'
condition: []
action:
  ... etc ...

It’s important to remember that this Template Trigger will only trigger when the climate entity changes from some other state to one of the listed states and then remains in that state for at least 15 minutes. For example, offheat or fan_onlycool.


BTW, what’s the purpose of this automation? It’s normal for a climate entity to be in one of those listed states for a long time (days, weeks) so what is the reason for detecting when it’s in that state for a mere 15 minutes?

Actually their template wasn’t invalid, it looked it was just missing value_template. I added support for providing a list of values to is_state a bit ago to make that pattern easier:

Although you’re right that they don’t need to use the if/endif structure. That example with paulus should probably be changed in the docs as it believe it comes from a time when template outputs didn’t get real types.

EDIT: Oh I guess the docs show both forms and specifically say the example below is showing the stringified version. Makes sense I suppose.

2 Likes

Wow, I completely missed that PR! Great enhancement, it’s very useful! :+1:


In that case, the original template can be employed like this:

alias: example
trigger:
  - platform: template 
    value_template: "{{ is_state('climate.bedroom_ac', ['heat', 'fan_only', 'cool', 'dry', 'heat_cool']) }}"
    for: '00:15:00'
condition: []
action:
  ... etc ...

The template is valid.

I am aware of that.

My A/C units are actually mini splits for each room in my condo and I try to keep them off when I’m not in the room. I keep one on when my child is napping, but after their nap is over I want to turn it off.
So the desired automation would alert me if the A/C unit has been left on for a longer than their nap. He naps for about 3 hrs during the middle of the day, if the A/C unit has gone from off to heat and remained in that state for 4.5hrs, it could probably needs to be turned off. That’s the purpose. The 15 minutes is just a placeholder example. I was testing the template with 30 seconds.

Seeing that you know exactly how it all works, so what’s your theory why it’s not working?

BTW, when the unit stops heating, doesn’t it continue to report its state is heat? It should report off only if it’s actually changed to offstate. Or does the unit behave differently?

I knew something had to be wrong with that doc example.

Thanks, that got me my desired result. I appreciate the help.

1 Like

Looks like I needed to edit the template in YAML.

I thought I was editting in YAML because when you click Template for trigger a text box comes up but it appears it’s still hiding details.

Visual editor:

Edit in YAML:

Also the example from the docs confused me with the if/endif. Its not needed at all.

I’m not sure what you mean by this.
Yes, it continues to report it’s state as heat, even when its not actively heating the room and its reached the set point.

Those old examples show how to explicitly report a boolean result yet, despite choosing to be verbose, stop short of including the false result.

A fully verbose example would be:

{% if whatever == 4 %}true{%else%}false{%endif%}

But the examples do a semi-verbose thing like this:

{% if whatever == 4 %}true{%endif%}

Anyway, the non-verbose equivalent is:

{{ whatever == 4 }}

The result of that is implicitly true/false.

No worries about my questions about heat/off; not relevant anymore.

As for the Automation Editor in visual mode, it can’t visually depict a Jinja2 template so it offers a text-box for the template. That’s not actually YAML mode. When you switch the editor to YAML mode then you see the automation in its true YAML form.

Be advised that Automation Editor has various quirks that become apparent when you attempt to do more advanced things. Some of those quirks work against you (like when it automatically quotes a boolean value thereby converting it to a string).

I certainly learned that today. Probably best to click Edit in YAML anytime a template is involved.

I appreciate your help with this automation.