Print entities as automation trigger

Hello again!
Does someone know why the following doesn’t work as an automation trigger?

platform: state
entity_id: >-
  {{ states.sensor|map(attribute='entity_id')|select("search",
  "linkquality")|join("")|replace ("sensor", "- sensor") }}
to: unavailable

It should

Nope. The entity_id option of a state trigger only accepts an entity. Not a template.

Use a template trigger.

Ok. But with a template trigger I cannot easily have a dynamic list of entities, I guess?
Is it for this reason?

The reason is that templates are not available on the entity_id field in any trigger. You can still use a template trigger and it will still contain an offending entity_id that caused the trigger. So just change to that.

I‘m afraid I can’t follow you.
A template trigger gives yes or no. How can I implement a dynamic list of entities there?
And what do you mean by offending entities?

by returning a yes or no based on what you’re looking for from the list. Template triggers contain a field entity_id that triggered the template.

I’m sorry, but this is the best I can come up with.

{% if is_state(states.sensor|map(attribute='entity_id')|select("search","linkquality")|list, 'unavailable') %}true{% endif %}"

I doubt this will work. Can you please give me another advice for directions?

are you using the template editor to play with your templates?

Yes, sure.

Copy-paste this into the Template Editor:

{{ states.sensor | map(attribute='entity_id')
  | select('search', 'linkquality')
  | selectattr('state', 'eq', 'unavailable')
  | list | count > 0 }}

However, this kind of template isn’t particularly useful for a Template Trigger. Why? Because it will trigger when template’s result changes from false to true and won’t trigger again until it first changes back to false. That’s how a Template Trigger works.

So if the count of unavailable sensors increases from 0 to 1 it will trigger but if the count continues to increase above 1 it won’t trigger again. The count has to first decrease to 0 to “reset” the Template Trigger.

1 Like

Template sensor outputting the count and trigger off the count changing.

1 Like

Despite my best intentions to explain the limitation of a Template Trigger, I overlooked to mention how to mitigate it (with a Template Sensor). :man_facepalming:

FWIW, I even recently showed someone how to do the same thing with a Template Sensor that reports which entity recently changed state (with some limitations).

2 Likes

Holy guacamoly! After reading this seems to be a very promising approach! Especially the service to set a group, didn’t know about that.
I could have updated the group every night with the template and your approach with template sensor is more than enough. I’ll try it and will let you know! Thanks a lot

@123 It works! Still need to fine-tune, but it definitely works, amazing!

my sensor:

- sensor:
  - name: Latest Zigbee Device Offline
    state: >
      {% set x = expand('group.zigbee_devices') | selectattr('state', 'eq', 'unavailable') | sort(attribute='last_changed', reverse=true) | list %}
      {{ (x[0].entity_id if now() - x[0].last_changed < timedelta(seconds=3) else '') if x | count > 0 else '' }}

the group creation script:

alias: Zigbee Offline Group Create
sequence:
  - service: group.set
    data:
      object_id: zigbee_devices
      entities: >
        {{ states.sensor | map(attribute='entity_id')
        |select("search","linkquality") |list }}
mode: single

The notification (or what I will do with it in the future)

alias: TEST Tell which zigbee offline
description: ''
trigger:
  - platform: state
    entity_id: sensor.latest_zigbee_device_offline
condition:
  - condition: template
    value_template: '{{ trigger.to_state.state != '''' }}'
action:
  - service: notify.mobile_app
    data:
      message: '{{ trigger.to_state.state }}'
mode: single

Thanks!

1 Like