Dynamic Notifier Messages

I’ve written a script which will notify me when the alarm is Armed and if any of the sensors are open/on to send me a notification.

The below script is currently working.

- id: '1539118763423'
  alias: Alarm Armed but Switch is Open
  trigger:
  - entity_id: alarm_control_panel.ha_alarm
    from: pending
    platform: state
    to: armed_away
  condition:
  - condition: or
    conditions:
    - condition: state
      entity_id: binary_sensor.0x00158d00022f1391
      state: 'on'
    - condition: state
      entity_id: binary_sensor.0x00158d00022da8a7
      state: 'on'
    - condition: state
      entity_id: binary_sensor.0x00158d00022f14e3
      state: 'on'
    - condition: state
      entity_id: binary_sensor.0x00158d000238c7ca
      state: 'on'
  - condition: state
    entity_id: alarm_control_panel.ha_alarm
    state: armed_away
  action:
  - data:
      message: " The Alarm has been turned on but a switch is open 1. "
      title: Warning - Alarm is Armed but a Switch is Open
    service: notify.gmail

Questions 1)
However, I would ideally like the notifier message to tell me which entity\sensor is on?
Is this possible? If so, how do I write something along these lines please.

Question 2)
If this is not possible, what is the best way for me to send myself a message with the current states of the listed sensors?

Using the guide found here: https://www.home-assistant.io/cookbook/notify_if_over_threshold/

I have written the following:

  action:
  - data:
      message: " The Alarm has been turned on but a switch is open 1. {{ states.sensor.[0x00158d0001e7932d].state }} "
      title: Warning - Alarm is Armed but a Switch is Open
    service: notify.gmail

But this does not work and the email fails to send. :frowning:

Firstly, need to use data_template, not data if you want to evaluate states.

Providing your email’s are working, what about:

  action:
  - data_template:
    message: >
      The Alarm has been turned on but a switch(es) {%- if (states.binary_sensor.0x00158d00022f1391.state == 'on') %} 1 
      {%- elif (states.binary_sensor.0x00158d00022da8a7.state == 'on') %} 2
      {%- elif (states.binary_sensor.0x00158d00022f14e3.state == 'on') %} 3
      {%- elif (states.binary_sensor.0x00158d000238c7ca.state == 'on') %} 4
      {% endif %} are on.

      title: Warning - Alarm is Armed but a Switch is Open
    service: notify.gmail

If you want to fix the grammar, and put proper commas and plurals (as/is), you would need to do the same and do some counts. to decide which to use.

Learned something new (I think.) I.e., the notify.xxx service accepts templates for message and title, even without data_template! So, e.g., you can use a template for message and/or title on the Services page.

Anyway, the error, then, is because the template you used:

" The Alarm has been turned on but a switch is open 1. {{ states.sensor.[0x00158d0001e7932d].state }} "

is invalid. It should have been:

" The Alarm has been turned on but a switch is open 1. {{ states.binary_sensor[0x00158d0001e7932d].state }} "

or

" The Alarm has been turned on but a switch is open 1. {{ states('binary_sensor.0x00158d0001e7932d') }} "

Note that you should avoid object_id’s (i.e., the part after the dot in the entity_id) that start with numbers if possible. Makes life easier. :wink:

If you want to automatically generate a list of friendly_names for the binary_sensors in the conditions that are on, you could do something like this:

  action:
  - data:
      message: >
        The Alarm has been turned on but one or more switches are open: {%
        set object_ids = ('0x00158d00022f1391', '0x00158d00022da8a7',
                          '0x00158d00022f14e3', '0x00158d000238c7ca') -%}
        {{ states.binary_sensor|selectattr('object_id','in',object_ids)
           |selectattr('state','eq','on')|map(attribute='name')|join(', ') }}
      title: Warning - Alarm is Armed but a Switch is Open
    service: notify.gmail
1 Like

@pnbruckner I’m not sure I completely understand the logic that you’ve written if I am completely honest with you.

All I know is that I took your logic, copy and pasted it in my automations file and it worked!!! Thank you so much. I’m blown away by this.

For anyone else interested, this is final working script for me:

- id: '1539118763423'
  alias: Alarm Armed but Switch is Open
  trigger:
  - entity_id: alarm_control_panel.ha_alarm
    from: pending
    platform: state
    to: armed_away
  condition:
  - condition: or
    conditions:
    - condition: state
      entity_id: binary_sensor.0x00158d00022f1391
      state: 'on'
    - condition: state
      entity_id: binary_sensor.0x00158d00022da8a7
      state: 'on'
    - condition: state
      entity_id: binary_sensor.0x00158d00022f14e3
      state: 'on'
    - condition: state
      entity_id: binary_sensor.0x00158d000238c7ca
      state: 'on'
  - condition: state
    entity_id: alarm_control_panel.ha_alarm
    state: armed_away
  action:
  - data:
      message: >
        The Alarm has been turned on but one or more switches are open: {%
        set object_ids = ('0x00158d00022f1391', '0x00158d00022da8a7',
                          '0x00158d00022f14e3', '0x00158d000238c7ca') -%}
        {{ states.binary_sensor|selectattr('object_id','in',object_ids)
           |selectattr('state','eq','on')|map(attribute='name')|join(', ') }}
      title: Warning - Alarm is Armed but a Switch is Open
    service: notify.gmail
1 Like

First it sets a variable named object_ids to a sequence (just think of it as a list) with the object_id’s of the binary_sensors in your conditions. Then it takes the states of all the binary_sensors (i.e., states.binary_sensor), and selects only those whose object_id is in the object_ids sequence variable using the selectattr filter. Of those it then selects only the ones whose state is equal to on (using the selectattr filter again). Then for all of those it outputs their name attribute (which is either the friendly_name, or if the entity doesn’t have a friendly_name, then it outputs the entity_id) using the map filter. Then it takes all of those names and if there is more than one joins them together using the string ', ’ using the join filter.

If you’re not familiar with these you can read more about them in the Jinja 2 docs.

2 Likes

Fantastic - thank you. I will certainly be reading into the document you have listed. This is my second week learning the language, I’m beginning to feel comfortable with the basics finally.

I’m also now going to see if I replicate this template for lights, it shouldn’t be too difficult. :wink:
(Famous last words)

1 Like