Automation template action problem

Hi!

I’m struggling trying to set this template automation. Everything works fine, except that what I want the message to report is which contact device is open and instead what I get is the name of the control panel name: “The alarm wasn’t set because the Home Alarm is open.”

Can someone point me how to get the friendly name of the device/s that is/are open instead of the name of the alarm control panel?

Thanks!!

- id: '1597425524429'
  alias: WARNING - Open doors or windows when triying to arm the alarm as away
  description: ''
  trigger:
  - entity_id: alarm_control_panel.home_alarm
    from: disarmed
    platform: state
    to: armed_away
  condition:
  - condition: and
    conditions:
    - condition: or
      conditions:
      - condition: device
        device_id: de85bc2b59cb4e248532155dc4d7dc2b
        domain: binary_sensor
        entity_id: binary_sensor.0x00158d0003f832ab_contact
        type: is_open
      - condition: device
        device_id: 03d073147eb449b68afdd358e5bbde4b
        domain: binary_sensor
        entity_id: binary_sensor.0x00158d0003d0c0ec_contact
        type: is_open
      - condition: device
        device_id: eba34eca164445d7991ad1db1bb21cf2
        domain: binary_sensor
        entity_id: binary_sensor.0x00158d0004080fa7_contact
        type: is_open
      - condition: device
        device_id: fc0ab22416fa4122accf6d7fc0b826e5
        domain: binary_sensor
        entity_id: binary_sensor.0x00158d0003f83343_contact
        type: is_open
      - condition: device
        device_id: 8c53691cded441f8adfafcd3a5e2f550
        domain: binary_sensor
        entity_id: binary_sensor.0x00158d0003d0ac0d_contact
        type: is_open
      - condition: device
        device_id: 863c4a0d6a624c73ac1a7cfe64a8d0a7
        domain: binary_sensor
        entity_id: binary_sensor.0x00158d0003f832e9_contact
        type: is_open
      - condition: device
        device_id: e185963076cc4a2cad81dba885f2b186
        domain: binary_sensor
        entity_id: binary_sensor.0x00158d000407ddb1_contact
        type: is_open
      - condition: device
        device_id: c643e09bc2e54bc39c42be2c64970788
        domain: binary_sensor
        entity_id: binary_sensor.0x00158d0003f832b8_contact
        type: is_open
      - condition: device
        device_id: a127042f025f4e34bf6ec17808b8977b
        domain: binary_sensor
        entity_id: binary_sensor.0x00158d00041a5f4b_contact
        type: is_open
  action:
  - data: {}
    entity_id: alarm_control_panel.home_alarm
    service: alarm_control_panel.alarm_disarm
  - data_template:
      message: 'The alarm wasn't set because the *{{ trigger.from_state.name
        }}* is open.

        '
      title: '*HOME ALARM*'
    service: notify.telegram
  mode: single

What you want, can’t be done the way you’re doing it.

The Trigger State Object is related to the automation’s trigger, not its conditions. The only entity that triggers this automation is alarm_control_panel.home_alarm so that’s the only entity known to the Trigger State Object. That’s why trigger.from_state.name reports “Home Alarm” and cannot reference any of the entities used in the condition.

If the goal is to prevent arming if there are open windows and doors, here’s how I handled it: I do it all via the UI. I have a Security page that lists all the open doors and windows (personal choice: it doesn’t show closed doors and windows, just the open ones). If anything is open, the card to arm the security system is hidden. After doors and windows are closed, the card to arm the system is displayed. The thinking here is that you don’t display the arming controls if the system isn’t ready to be armed.

I also display a sensor that simply reports if the security system is ready to be armed or not. That’s just a convenience in case one stares at the screen and wonders where is the card to arm the system?

1 Like

That makes sense! I was almost sure that There was no problem with the script since if I removed the template from the message, the automation worked perfectly. Thanks a lot, Taras!

Could she use a group and something like this either in the action or sensor?

expand('group.my_sensors') | selectattr('state', 'eq', 'is_open') | map_attribute('friendly_name') | list

This also looks very promising. I’m going to try it this afternoon and report back with the results just in case this helps someone in the future. Thanks a lot, dave85stmn!

For that template, if used in the automation’s trigger, the Trigger State Object will be for group.my_sensors and not for members of the group.

For what I was thinking she could use that list instead of the trigger.thing she was originally trying

I think you’r almost there, but it reports the error “Message malformed: invalid template (TemplateAssertionError: no filter named ‘map_attribute’) for dictionary value @ data[‘action’][1][‘data_template’][‘message’]” when I try to save it like this at the action template:

  action:
  - data: {}
    entity_id: alarm_control_panel.home_alarm
    service: alarm_control_panel.alarm_disarm
  - data_template:
      message: 'The alarm wasn't set because the *{{ expand('group.contacts') | selectattr('state', 'eq', 'is_open') | map_attribute('friendly_name') | list }}* is open.

        '
      title: '*HOME ALARM*'
    service: notify.telegram
  mode: single

I really appreciate your input, sice I know nothing of templating.

  action:
  - data: {}
    entity_id: alarm_control_panel.home_alarm
    service: alarm_control_panel.alarm_disarm
  - data_template:
      message: >-
        The alarm wasn't set because the following are open: {{ expand('group.contacts') | selectattr('state', 'eq', 'on') | map(attribute='attributes.friendly_name') | list | join(', ') }}
      title: '*HOME ALARM*'
    service: notify.telegram
  mode: single

I still encourage you to consider the approach I presented. In my opinion, it makes no sense to allow someone to arm a security system and then report it couldn’t do it. If it can’t be armed, don’t allow the user to arm it in the first place. Only allow arming when the security system can be successfully armed.

You are a genius!!! Thanks so much! That did the trick perfectly.

1 Like

The errors in the original template were:

  1. 'is_open' is not a valid state for binary_sensors (use 'on')
  2. map_attribute is not the name of any function (it’s map)
  3. In this situation, to access friendly_name you use attributes.friendly_name
  4. The last filter is list which will produce a python list. Use join(', ') to convert the python list’s to a comma-delimited string.

I wold really like to learn how to write templates, but I have read the documentation for jinja and the home asisstant information regardin templating and I have not understood nothing. Would you be so kind to recomend me where to start?

I suggest reading the documentation, specifically the section for Templating. Home Assistant uses Jinja2 for templating so you will need to become familiar with its features (note: not all of its capabilities are available in Home Assistant). In addition, I suggest doing what I did and that’s to review the examples posted in this community forum. Most of what I learned was by observing how other people created templates.

I personally use a HAdashboard firetablet panel for my alarm. I want to see if they also have conditional components. I like that idea. Until now I had to run a disarm command if not properly armed. I also need to work out the same for Alexa voice arming.

Yes, that’s exactly the steps I’ve been taking, but so far with no much luck.

Thanks a lot, 123 Taras!

Dave, this automation does exactly that. It won’t allow you to arm the alarm and it will text you which sensor is preventing the alarm to be armed.