hey all,
trying to create a automation that would notify me if any of my rads is to be permanently on.
i have 12 different rads, so didnt want to create a new alert for each one!
the status for the rad is set to an attribute.
my value_template when i test in the UI works (see image)
however i dont get any alerts with my automation? is this because my value_template doesnt return a true/false value maybe?
automation:
- alias: 'ALERT: Rad set to always on'
id: alart_rad_always_on
trigger:
- platform: template
value_template: &rad_setto_permanent >
{% macro rads_checker() %}
{% for entity_id in states.group.all_rads.attributes.entity_id if (
is_state_attr(entity_id, 'preset_mode', 'permanent')
) -%}
{{ state_attr(entity_id, "friendly_name") }} ({{ states(entity_id) }})
{% endfor -%}
{% endmacro %}
{{ rads_checker() }}
action:
- service: .create
data_template:
message: '{{ trigger.from_state.attributes.friendly_name }} is set to be permentatly on!'
title: Rad is permanently on!
- service: notify.notify
data_template:
message: '{{ trigger.from_state.attributes.friendly_name }} is set to be permentatly on!'
title: Rad is permanently on!
edit, i had forgot to add this: “| trim != “””
which then gave me the true/false value i needed
i still dont get any alerts however.
- alias: 'ALERT: Rad set to always on'
id: alart_rad_always_on
trigger:
- platform: template
value_template: &rad_setto_permanent >
{% macro rads_checker() %}
{% for entity_id in states.group.all_rads.attributes.entity_id if (
is_state_attr(entity_id, 'preset_mode', 'permanent')
) -%}
{{ state_attr(entity_id, "friendly_name") }} ({{ states(entity_id) }})
{% endfor -%}
{% endmacro %}
{{ rads_checker() | trim != "" }}
action:
- service: persistent_notification.create
data_template:
message: '{{ trigger.from_state.attributes.friendly_name }} is set to be permentatly on!'
title: Rad is permanently on!
- service: notify.notify
data_template:
message: '{{ trigger.from_state.attributes.friendly_name }} is set to be permentatly on!'
title: Rad is permanently on!
you’ll never get alerts because that value_template will only trigger when state changes occur to group.all_rads. I believe you’re expecting this to trigger when ever entities in the group update. The only way you can get that to happen is if you list all entity id’s in the template so that listeners can be created.
thanks for the replies. im slightly confused.
in these 2 examples below, which BOTH produce the result “true” - the 2nd one fires (works) the 1st one doesnt.
doesnt work but the value_template gives the same result
- alias: 'ALERT: Rad set to always on'
id: alart_rad_always_on
trigger:
- platform: template
value_template: >
{%- macro rads_checker() -%}
{%- for entity_id in states.group.all_rads.attributes.entity_id if (
is_state_attr(entity_id, 'preset_mode', 'permanent')) -%}
{{ state_attr(entity_id, "friendly_name") }}
{%- endfor %}
{%- endmacro -%}
"{{ rads_checker() | trim != "" | replace('\n', '') }}"
action:
- service: persistent_notification.create
data_template:
message: rad test we want !!
title: rad test we want !!
- service: notify.notify
data_template:
message: rad test we want !! #'{{ trigger.from_state.attributes.friendly_name }} is set to be permentatly on!'
title: rad test we want !!
works
- alias: 'ALERT: Rad set to always on2'
id: alart_rad_always_on2
trigger:
- platform: template
value_template: "{{ is_state_attr('climate.bar', 'preset_mode', 'permanent') }}"
action:
- service: persistent_notification.create
data_template:
message: test3
title: Rad is permanently on3!
- service: notify.notify
data_template:
message: test3 #'{{ trigger.from_state.attributes.friendly_name }} is set to be permentatly on!'
title: Rad is permanently on3!
NOTE:
In the tests I performed, you may need to use 'Permanent' (first letter capitalized) if the template fails to work as shown above. A simple way to test if it’s needed is to paste this into the Template Editor and try 'permanent' and 'Permanent'.
You may have noticed I used states.climate in the very last template. That represents all the State objects of every climate component you’ve defined. That template works in the Template Editor but not in a Template Trigger (or the value_template of a Template Sensor).
Why not? Because, in a Template Trigger, Home Assistant doesn’t automatically expand states.climate and then monitor each member for state-changes. It would be cool if it did, but it doesn’t.
You created group.all_rads. Paste this into the Template Editor and you’ll see it works equally well:
That expand function sure looks handy. Maybe it works in a Template Trigger? Nope, not the way we would want. What Home Assistant sees is group.all_rads and will monitor it for state-changes and not the group’s members (which would be more useful for our purposes).
If you think it would be useful to have Home Assistant monitor the group’s members (when it sees expand used in a Template Trigger) then you may wish to cast a vote for this Feature Request: