On startup, Home Assistant inspects the Template Sensor’s template and attempts to identify it’s entities. For each identifiable entity it creates a “listener”. The listener is responsible for detecting the entity’s state changes. If it cannot find any entities, no listeners are created. The result is the Template Sensor’s template is evaluated once on startup and never again (until the next restart). The only way to ensure Home Assistant can create listeners is to include the desired entities within the template, or separately in the entity_id option (which supercedes the template).
The automation you linked to works with events, not entity states, and is not applicable to Template Sensors.
you could create an automatic group with the conditions you like, and then listen to the expanded group. I do something like that with my batteries
script:
group_battery_sensors_low_create:
alias: Group battery sensors low create
sequence:
service: group.set
data_template:
object_id: battery_sensors_low
entities: >-
{%- for s in states.sensor
if ('battery_level' in s.entity_id or
'motion_sensor_battery' in s.entity_id)
and s.state|int < states('input_number.battery_alert_level')|int %}
{{s.entity_id}}{% if not loop.last %}, {% endif %}
{%- endfor %}
call this script using an automation which can be triggered at startup and other triggers.
or use template sensors like this;
low_level_batteries:
friendly_name: Low level batteries
value_template: >
{%- set ns = namespace(below=[]) %}
{%- for s in states.sensor
if ('battery_level' in s.entity_id or 'motion_sensor_battery' in s.entity_id)
and s.state|int < states('input_number.battery_alert_level')|int
and s.state != 'unknown' %}
{%- set ns.below = ns.below + [s] %}
{%- endfor %}
{{ns.below|count}}
attribute_templates:
List: >
{%- set ns = namespace(below=[]) %}
{%- for s in states.sensor
if ('battery_level' in s.entity_id or 'motion_sensor_battery' in s.entity_id)
and s.state|int < states('input_number.battery_alert_level')|int
and s.state != 'unknown' %}
{%- set ns.below = ns.below + [s.name + ': (' + s.state + '%)'] %}
{%- endfor %}
{% set count_below = ns.below|count|int %}
{% set list = ns.below %}
{%- if count_below == 0 %} All batteries are fine
{%- elif count_below == 1 %}
{{list[0]}} is low!
{%- elif count_below == 2 %}
{{list|join(' and ') }} are low!
{%- else %}
{{list[:-1]|join(', ')}}, and {{list[-1]}} are low!
{%- endif %}
binary_sensor:
- platform: template
sensors:
battery_alert:
friendly_name: Battery alert
device_class: safety
value_template: >
{{states('sensor.low_level_batteries')|int > 0}}
attribute_templates:
Count: >
{{states('sensor.low_level_batteries')|int }}
List: >
{{state_attr('sensor.low_level_batteries','List')}}
There is work underway to improve Home Assistant’s ability to identify entities in a template. In some future release, listeners for each binary_sensor will be created for the following:
What are the identifiable entities in sensor.low_level_batteries?
The reason why I ask is because I tried to use this in a Template Sensor in 0.114 (as an experiment) and it was reported in the log to have no identifiable entities.
It has always worked in the Template Editor and only because you are refreshing the template manually.
It won’t work in a Template Sensor because no listeners will be created for states.light. This ability is currently being developed but doesn’t yet exist in any release version.
# This one doesn't listen to anything and is flagged in the log
v114_states:
friendly_name: 114 States
value_template: >
{{ states.light | selectattr('state','eq', 'on') | list | length }}
depending on the needs of the poster, he might be able to use the group’s all: functionality to his advantage, and automate based on the state of that group, instead of all individual entities.
If these need to be available to use {{trigger.to_state.state}} for now there’s no other option than to list them in the trigger: platform: state as individual entities.
I’d hope using that group as a triggering entity would someday have the option to trigger on all containing entities in that group.
Originally (back in version 0.110) the new functionality was simply to assign listeners to all members of a group when used with expand. So if the following template were used in a Template Sensor, Home Assistant would listen to all entities within the group (and not only the group itself):
{{ expand('group.my_sensors') | etc }}
Unfortunately, it didn’t work. Changes were made in 0.114 but those also failed to make it work.
There’s a completely separate initiative underway to fundamentally change the way Home Assistant assigns listeners to a Template Sensor. It’s goal is not only to handle expand correctly, it will also support states.light, states.sensor, etc. In theory, it should be able to handle something tricky like this: