Help - macro in a template?

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?

i looked at what was done for batteries here and tried to follow this logic, however im stuck: https://github.com/notoriousbdg/Home-AssistantConfig/blob/3adce961312c018ffff733d8e581e77cde2d9e8a/packages/battery_alert.yaml#L295-L338

group:
all_rads:
  name: all_rads 
  entities:
    - climate.bar
    - climate.bathroom
    - climate.bedroom
    - climate.briony_office
    - climate.en_suite
    - climate.guest_room
    - climate.gym
    - climate.hallway
    - climate.jamie_office
    - climate.kitchen
    - climate.landing
    - climate.livng_room
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.

Withdrawn. Ninja’d by petro.

:man_cartwheeling:

2 Likes

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!

Second one has an identifiable entity that Home Assistant can listen for state-changes.

ok, i need to read up more then to understand this, will stop playing with it.

any other way to do this, other then spliting each Rad into it own binary_sensor?

Try this:

- alias: 'rad alert test'
  id: rad_alert_test
  trigger:
    platform: template
    value_template: >
      {% set rads = [states.climate.bar,
                     states.climate.bathroom,
                     states.climate.bedroom,
                     states.climate.briony_office,
                     states.climate.en_suite,
                     states.climate.guest_room,
                     states.climate.gym,
                     states.climate.hallway,
                     states.climate.jamie_office,
                     states.climate.kitchen,
                     states.climate.landing,
                     states.climate.livng_room] %}
      {{ rads | selectattr('attributes.preset_mode', 'eq', 'permanent') | list | count > 0 }}
  action:
    service: persistent_notification.create
    data_template:
      message: '{{ trigger.to_state.attributes.friendly_name }} is permanently on.'
      title: Rad is permanently on!

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'.

{{ states.climate | selectattr('attributes.preset_mode', 'eq', 'permanent') | list | count  }}

EDIT
Correction. Added missing %} to the first statement in the template.

1 Like

worked great (had to add in %} at th end of the set)

thanks so much!!!

so much to learn…i spent 2h playing with this and you knock it out in <10min :persevere:

1 Like

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:

{{ expand('group.all_rads') | selectattr('attributes.preset_mode', 'eq', 'permanent') | list | count  }}

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:

1 Like

thanks for the info! +1 vote added