I can't get a template trigger to work in an automation

I am trying to trigger an automation off of the following template value-templates:

“{{ is_state_attr(‘fan.fanlinc_51_d1_37_fan’, ‘percentage’, 100) }}”

“{{ is_state_attr(‘fan.fanlinc_51_d1_37_fan’, ‘percentage’, 66) }}”

“{{ is_state_attr(‘fan.fanlinc_51_d1_37_fan’, ‘percentage’, 67) }}”

“{{ is_state_attr(‘fan.fanlinc_51_d1_37_fan’, ‘percentage’, 33) }}”

These are, of course, four separate triggers which would be in four separate automations, corresponding to “fan on high” for the first, “fan on medium” for the second and third, and “fan on low” for the fourth.

The problem is that the automations never trigger, even though the if I put any one of these in the template editor in developer tools, I get appropriate true or false values. Can anyone suggest what I might be doing wrong?

A related, but ultimately moot question, unless I can make some progress, is how could I combine the second and third into a single value-template that would be true for either 67 or 66. This is the "fan on medium, which sometimes returns 66 and sometimes returns 67. Some way of doing an “or” or a “between” would be useful. I am sure this is the easier of the two questions, but it’s so closely related that I thought I would put it here.

Background information:

  1. This percentage is a state attribute of an Insteon fanlinc fan entity, so this my problem may have something to do with the Insteon integration, but it seems more related to automations in general.

  2. I’ve tried triggering on the device state and adding the value-template as a condition, but the condition seems to prevent the automation from triggering, so no joy there.

Any help appreciated. Thanks.

There isn’t anything obviously wrong with your templates. Please post the actual automation (properly formatted) and debug logs so we can see everything in context.

trigger:
  - platform: template
    value_template: "{{ 66 <= state_attr('fan.fanlinc_51_d1_37_fan', 'percentage') <= 67 }"

It does not have to be 4 separate automation. Triggers are OR. Conditions are AND.

Something like this would get all 4 conditions.

trigger:
  - platform: template
    value_template: "{{ state_attr('fan.fanlinc_51_d1_37_fan', 'percentage') | int(0) => 99 }"
  - platform: template
    value_template: "{{ 65 <= state_attr('fan.fanlinc_51_d1_37_fan', 'percentage') | int(0) <= 67 }"
  - platform: template
    value_template: "{{ state_attr('fan.fanlinc_51_d1_37_fan', 'percentage')  | int(0) <= 33 }"

Accounted for truncation on fan speed medium - why the 65.

alias: Fan on medium
description: ''
trigger:
  - platform: template
    value_template: >-
      "{{ 50 <= state_attr('fan.fanlinc_51_d1_37_fan', 'percentage') | int(0) <=
      80 }}"
condition: []
action:
  - type: turn_off
    device_id: cc3371d85a465d8bbb7c8e9204bdf00f
    entity_id: switch.keypad_with_dimmer_53_b6_76_button_d
    domain: switch
  - type: turn_off
    device_id: cc3371d85a465d8bbb7c8e9204bdf00f
    entity_id: switch.keypad_with_dimmer_53_b6_76_button_a
    domain: switch
  - type: turn_off
    device_id: cc3371d85a465d8bbb7c8e9204bdf00f
    entity_id: switch.keypad_with_dimmer_53_b6_76_button_c
    domain: switch
  - type: turn_on
    device_id: cc3371d85a465d8bbb7c8e9204bdf00f
    entity_id: switch.keypad_with_dimmer_53_b6_76_button_b
    domain: switch
mode: single

debug log:

2022-04-24 12:04:28 INFO (MainThread) [homeassistant.components.automation.fan_on_medium] Initialized trigger Fan on medium
2022-04-24 12:04:42 DEBUG (MainThread) [homeassistant.components.insteon.insteon_entity] Received update for device 51d137 group 2 value 0
2022-04-24 12:04:47 DEBUG (MainThread) [homeassistant.components.insteon.insteon_entity] Received update for device 51d137 group 2 value 169

The info entry was after I modified the trigger based on advice from J Gent on how to have a range. The two debug entries were after I used the home assistant app on ios to turn the fan off and then on at percentage 67

and here is the result with the template in developer tools:

I noticed you said you needed 4 automation. You can do it with one as follows:

alias: Fan on medium
description: ''
trigger:
  - id: 'high'
    platform: template
    value_template: >-
      "{{ state_attr('fan.fanlinc_51_d1_37_fan', 'percentage') | int(0) > 80 }}"
  - id: 'medium'
    platform: template
    value_template: >-
      "{{ 50 <= state_attr('fan.fanlinc_51_d1_37_fan', 'percentage') | int(0) <= 80 }}"
  - id: 'low'
    platform: template
    value_template: >-
      "{{ state_attr('fan.fanlinc_51_d1_37_fan', 'percentage') | int(0) < 50 }}"
condition: []
action:
- choose:
  - conditions:
    - "{{ trigger.id == 'high' }}"
    sequence:
  - service: switch.turn_off
    target:
      entity_id: 
      - switch.keypad_with_dimmer_53_b6_76_button_d
      - switch.keypad_with_dimmer_53_b6_76_button_a
      - switch.keypad_with_dimmer_53_b6_76_button_c
  - service: switch.turn_on
    target:
      entity_id: 
      - switch.keypad_with_dimmer_53_b6_76_button_b
  - conditions:
    - "{{ trigger.id == 'medium' }}"
    sequence:
  - service: switch.turn_off
    target:
      entity_id: 
      - switch.keypad_with_dimmer_53_b6_76_button_d
  - service: switch.turn_on
    target:
      entity_id: 
      - switch.keypad_with_dimmer_53_b6_76_button_b
      - switch.keypad_with_dimmer_53_b6_76_button_a
      - switch.keypad_with_dimmer_53_b6_76_button_c
  - conditions:
    - "{{ trigger.id == 'low' }}"
    sequence:
  - service: switch.turn_off
    target:
      entity_id: 
      - switch.keypad_with_dimmer_53_b6_76_button_c
 - service: switch.turn_on
    target:
      entity_id: 
      - switch.keypad_with_dimmer_53_b6_76_button_b
      - switch.keypad_with_dimmer_53_b6_76_button_d
      - switch.keypad_with_dimmer_53_b6_76_button_a
default: []
mode: single

the trigger_id’s are a wonderful enhancement to reduce the number of automations. Also using services is simpler than using devices and easier to maintain.

1 Like

Yes, very nice improvement. Thanks, I am learning a lot and I appreciate the help. But, of course, if I can’t get it to trigger on a template, the nicer code is moot.

Barry

Might be that the automation only ‘triggers’ when the template CHANGES to ‘true’.

It’s a subtle, yet important point with automations.

TRIGGER. What CHANGE needs to occur to trigger the automation.

CONDITION. What needs to be true for the automation actions to run?

ACTION. What should happen once triggered and when conditions pass.

Further worth noting you can have logic in the action piece (as noted above with trigger id’s), or other ways.

TBH. When I first started w/ HA, it’s easier to just do a single automation for each trigger vs. trying to combine a bunch of different triggers into a single automation. The forums will ‘default’ to having you combine them, or finding creative/‘fun’ ways to combine them, but it’s typically more of a mental exercise (CAN this be done?!?) than a true performance/time saving benefit.

My advice, have multiple automations and trigger each individually with their own set of conditions / actions. Keeps it simpler to start and then, if you want, you can work to combine down the road.

There just really isn’t a huge downside, other than ‘extra’ automations in your automations list to combining…

I agree about the multiple automations, at least for getting it to work initially. But I do want to give credit for the suggestion about how to combine the four into one.

So, how do you know what CHANGE needs to occur to trigger a template automation.

I mean the only reason I am using a template is because the thing that I want to trigger on is a state attribute. If a change in the attribute doesn’t trigger it, and even a change in state doesn’t trigger it, then I am having a hard time seeing the use of even having a template trigger.

At the end of the day, if I’m not doing something wrong, and it never triggers, then I will make it an issue, but, being a newbie, I wanted some advice on whether I am misunderstanding this before going that route.

So, at his point I have gotten advice on how to make my non-working automation more elegant, and some hints on why it might not be triggering, I guess I need to read more about template triggers to know the answer to the “what CHANGE question.” Is that something that could be integration dependent?

The change I’m referring to is if the value goes from below the test to above the test

Ie if I had a thermostat that reported humidity as a state attribute and that humidity went from below 60 to above 60 then that would be a trigger

If it went from 61 to 62 it wouldn’t be a trigger because it didn’t go THROUGH the set threshold of 60

I also believe that if you create the automation using the UI, it allows you to select the attribute that you want your automation to trigger upon.

So without getting too deep into yamil or templates you should just be able to create these in the UI

Thanks for clarifying. That is not the problem here. Although the fan speed is expressed as a percentage in the template there are actually only 4 or 5 values it can have.

Zero for fan off, and for fan off I can trigger on device and check the entity state in the UI as you suggested where I get a choice of turned off, turned on, or toggled. This works in that the entity state change does cause the device to trigger.

But there are three on states. Low which has a percentage of 33, Medium which has percentage 66 or 67 and High which has a percentage of 100. The only choices in a device trigger are on off and toggle. The percentage is a state attribute which I can see in developer tools / states. And I can see it change on the UI on my computer web browser as I turn on the fan to a particular speed from my phone app. But the automation does not trigger.

You guess it’s time to open an issue.

Barry

It’s explained in the documentation.

The template’s result must change from boolean false to true or, if the result is numeric, it must change to a non-zero numeric value.

In your case, the templates produce booleans so they must change from false to true in order to trigger (it’s the state-change that initiates the trigger).

After it changes from false to true it must first revert to false before it can change to true to initiate another trigger (in other words, true to true isn’t a state-change so won’t trigger).

That makes sense and since that is happening as verified in developer tools and the automation is not triggering, I’ve opened an issue. Thanks for all the help you’ve all given me. I didn’t want to open an issue until I had done my best to make sure I was not making a mistake. But now I am there.

How are you using Developer Tools to confirm the operation of a Template Trigger?

It can be used to confirm the behavior of a template but that’s not quite the same thing as a Template Trigger.

Would something like this work? It triggers on a change in attribute percentage, Uses conditions to see where, then executes the code. This should fire on any change in percentage.

alias: Fan on based on conditions
description: ''
trigger:
  - platform: state
    entity: fan.fanlinc_51_d1_37_fan
    attribute: percentage
condition: []
action:
- choose:
  - condition: template
    value_template:  "{{ state_attr('fan.fanlinc_51_d1_37_fan', 'percentage') | int(0) > 80 }}"
    sequence:
  - service: switch.turn_off
    target:
      entity_id: 
      - switch.keypad_with_dimmer_53_b6_76_button_d
      - switch.keypad_with_dimmer_53_b6_76_button_a
      - switch.keypad_with_dimmer_53_b6_76_button_c
  - service: switch.turn_on
    target:
      entity_id: 
      - switch.keypad_with_dimmer_53_b6_76_button_b
  - condition: template
    value_template:  "{{ 50 <= state_attr('fan.fanlinc_51_d1_37_fan', 'percentage') | int(0) <= 80 }}"
    sequence:
  - service: switch.turn_off
    target:
      entity_id: 
      - switch.keypad_with_dimmer_53_b6_76_button_d
  - service: switch.turn_on
    target:
      entity_id: 
      - switch.keypad_with_dimmer_53_b6_76_button_b
      - switch.keypad_with_dimmer_53_b6_76_button_a
      - switch.keypad_with_dimmer_53_b6_76_button_c
  - condition: template
    value_template: "{{ state_attr('fan.fanlinc_51_d1_37_fan', 'percentage') | int(0) < 50 }}"
    sequence:
  - service: switch.turn_off
    target:
      entity_id: 
      - switch.keypad_with_dimmer_53_b6_76_button_c
 - service: switch.turn_on
    target:
      entity_id: 
      - switch.keypad_with_dimmer_53_b6_76_button_b
      - switch.keypad_with_dimmer_53_b6_76_button_d
      - switch.keypad_with_dimmer_53_b6_76_button_a
  default: []
mode: single

Hi Taras,

I am confirming the value of the template (ie true or false) in developer tools. The fact that it does not trigger can be seen in the automations part of configuration.

Barry

Something very much like that is in fact working:

It was proposed by Tom Harris (teharris1) the code owner for the Insteon Integration after I opened an issue. His example had to do with state of dimming of a lighjt. I modified it for the fan and it worked, which is great. But I still don’t understand why the Template trigger doesn’t work.

Barry

alias: Fan on state trigger
description: ''
trigger:
  - platform: state
    entity_id: fan.fanlinc_51_d1_37_fan
    attribute: percentage
condition: []
action:
  - variables:
      fs_pct_val: '{{ state_attr(''fan.fanlinc_51_d1_37_fan'', ''percentage'') | int }}'
  - service: switch.turn_off
    data: {}
    target:
      entity_id:
        - switch.keypad_with_dimmer_53_b6_76_button_a
        - switch.keypad_with_dimmer_53_b6_76_button_b
        - switch.keypad_with_dimmer_53_b6_76_button_c
        - switch.keypad_with_dimmer_53_b6_76_button_d
  - service: switch.turn_on
    data_template:
      entity_id: |
        {% if fs_pct_val == 0 %}
          switch.keypad_with_dimmer_53_b6_76_button_d
        {% elif fs_pct_val <= 40 %}
          switch.keypad_with_dimmer_53_b6_76_button_c
        {% elif fs_pct_val <= 80 %}
          switch.keypad_with_dimmer_53_b6_76_button_b
        {% else %}
          switch.keypad_with_dimmer_53_b6_76_button_a
        {% endif %}
mode: single