Template Based Automation Doesn't Trigger

I’m working on building dashboard displays and notifications based on the Waste Collection Schedule integration. Everything works great, however I want an automation to prepare a input_text helper to support the displays and notifications.

Below is the YAML of what I have, and attached is an image of what the trace shows me. For whatever reason, even though one of the sensors referenced does fall into the trigger critera, the actions are skipped.

id: '1706717504758'
alias: System | Update Next Rubbish Collection
description: ''
trigger:
  - platform: template
    value_template: |-
      {% if states('sensor.rubbish_collection_pink') > 8 %}
      {% endif %}
    id: Green
  - platform: template
    value_template: |-
      {% if states('sensor.rubbish_collection_green') > 8 %}
      {% endif %}
    id: Pink
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - Green
        sequence:
          - service: input_text.set_value
            target:
              entity_id: input_text.home_next_rubbish_collection
            data:
              value: GREEN
      - conditions:
          - condition: trigger
            id:
              - Pink
        sequence:
          - service: input_text.set_value
            target:
              entity_id: input_text.home_next_rubbish_collection
            data:
              value: PINK and BLACK

Note I’ve also tried to set the trigger critera by referencing the entities themselves, not by template, and I get the same result.

What am I doing wrong?

Preformatted text

Please format your code

Code_format

From the trace, it looks like you executed the automation manually, didn’t you?
Then, there is no trigger being used, so all trigger conditions return false.

  1. What koying said. Executing an automation’s Run command only executes its actions. That means it skips the triggers. By skipping the triggers, the trigger variable is never defined. An undefined trigger variable means none of the Trigger Conditions will work.

  2. Your two Template Triggers will never trigger because their template is comparing a string to a number. In addition, the template uses an if - then that reports nothing.

I suggest you change your Template Triggers to this:

trigger:
  - platform: template
    value_template: "{{ states('sensor.rubbish_collection_pink') | int(0) > 8 }}"
    id: Green
  - platform: template
    value_template: "{{ states('sensor.rubbish_collection_green') | int(0) > 8 }}"
    id: Pink

NOTE

Your Template Triggers report Green when sensor.rubbish_collection_pink is greater than 8 and Pink when sensor.rubbish_collection_green is greater than 8. Is that intentional or a mistake?

That’s a great tip, thank you koying!

I’m new to all this so I appreciate you taking the time!

I didn’t know this either, whats the best way of testing automations then?

Thanks Taras, I didn’t know how to convert the value to number for a like for like comparision, so I’ve added your suggestion code to the automation now. I’ve also switched the logic as you mentioned, which makes it more sense.

trigger:
  - platform: template
    value_template: |-
      {% if states('sensor.rubbish_collection_pink') | int(0) < 8 %}
      {% endif %}
    id: Pink
  - platform: template
    value_template: |-
      {% if states('sensor.rubbish_collection_green') | int(0) < 8 %}
      {% endif %}
    id: Green

I just need a way to trigger it as a test now - any suggestions?

Well, to test automations where triggers are meaningful, you have to actually manually trigger one of the triggers, somehow, or add test ones.

For your case, you could, e.g., create a test helper sensor that you would add to the list of trigger.

After doing the changes suggested by Taras, that is. You original code (that you still haven’t properly formatted) seems wrong.

That code doesn’t output any values. Use the templates @123 suggested, do not doctor them.

1 Like

Test if the sensor is less than 8, and…

As petro says, this outputs nothing: not true/false, nothing at all.

Taras’s template (adjusted for your less-than-8):

{{ states('sensor.rubbish_collection_pink') | int(0) < 8 }}

returns true or false depending on the comparison, and the template trigger fires off that value.

Some bedtime reading:

  1. The revised version of what you posted still uses an if endif structure which won’t work the way you think it might. Use the template I suggested.

  2. In your original version, the template tests if the sensor’s value is greater than 8. In your revised version it tests if it’s less than 8. That’s a significant change to the test. Which version of the test is correct?

Assuming that the new version of the test is the correct one, here’s what I suggest you use:

id: '1706717504758'
alias: System | Update Next Rubbish Collection
description: ''
trigger:
  - platform: template
    value_template: "{{ states('sensor.rubbish_collection_pink') | int(0) < 8 }}"
    id: PINK and BLACK
  - platform: template
    value_template: "{{ states('sensor.rubbish_collection_green') | int(0) < 8 }}"
    id: GREEN
condition: []
action:
  - service: input_text.set_value
    target:
      entity_id: input_text.home_next_rubbish_collection
    data:
      value: "{{ trigger.id }}"

To test the automation, you can set the value of sensor.rubbish_collection_pink to a number lower than 8. You can do that using Developer Tools > States.

  1. Go to Developer Tools > States
  2. Find sensor.rubbish_collection_pink in the list and click it.
  3. Scroll to the top of the page where all of the sensor’s properties are displayed in a form.
  4. Take note of its current State value (perhaps make a copy of it). It should currently be above 8 in order for the test to work.
  5. Change the value to 7 or anything less than 8.
  6. Click the Set State button.
  7. That should be sufficient to trigger the automation’s Template Trigger.
  8. Optionally, use the same technique to set the sensor’s value back to its original value. Otherwise, simply wait for the sensor’s integration to update the sensor’s value.

Thanks all, this is a learning curve for sure!

The current automation looks like the following, and it fired on it’s own early yesterday but still appeared to skip the actions:

alias: System | Update Next Rubbish Collection
description: ""
trigger:
  - platform: template
    value_template: "{{ states('sensor.rubbish_collection_pink') | int(0) > 8 }}"
    id: Green
  - platform: template
    value_template: "{{ states('sensor.rubbish_collection_green') | int(0) > 8 }}"
    id: Pink
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - Green
        sequence:
          - service: input_text.set_value
            target:
              entity_id: input_text.home_next_rubbish_collection
            data:
              value: GREEN
      - conditions:
          - condition: trigger
            id:
              - Pink
        sequence:
          - service: input_text.set_value
            target:
              entity_id: input_text.home_next_rubbish_collection
            data:
              value: PINK and BLACK
mode: single

I’ll read the Templating guide this evening, but any additional tips in the meantime?

image

Not the right trace?