iOS Notification based on Doors left open

Heya,

I have managed to setup an automation that sends a basic notification to my iOS device when I am not at home and have left a door open. I’m now trying to build on the notification to include the specific door that was left open (see below):

- alias: 'Doors left open'
    hide_entity: true
    trigger:
      platform: state
      entity_id:  binary_sensor.jonathan_presence
      to: 'off'
    condition:
      condition: state
      entity_id: group.doors
      state: 'on'
    action:
      - service: notify.ios_ifone
        data:
          message: >
            {% set open_doors = {{ states | selectattr('entity_id', 'in', state_attr('group.all_doors','entity_id')) | selectattr('state','in',['on','open']) | list | map(attribute='name') | join(', ') }}
            The following doors have been left open: {{ open_doors }}

However this throws an error in the log:

2019-04-06 21:00:00 ERROR (MainThread) [homeassistant.components.automation] Error while executing automation automation.doors_left_open. Invalid data for call_service at pos 1: invalid template (TemplateSyntaxError: expected token ':', got '}') for dictionary value @ data['message']

Any suggestions on how I could make this work? Thanks

Do you have a group.all_doors? It isn’t a default group as far as I know.

{{ states | selectattr('entity_id', 'in', state_attr('group.door','entity_id')) | selectattr('state','in',['on','open']) | list | length >= 1 }}

{{ states | selectattr('entity_id', 'in', state_attr('group.door','entity_id')) | selectattr('state','in',['on','open']) | list | map(attribute='name') | join(', ') }}

I pasted the following into my template editor with my door group at it was able to work.

If you do have group.all_doors, try removing the argument within the set argument. There also wasn’t a %} to close the set.

{% set open_doors = states | selectattr('entity_id', 'in', state_attr('group.all_doors','entity_id')) | selectattr('state','in',['on','open']) | list | map(attribute='name') | join(', ') %}
The following doors have been left open: {{ open_doors }}

Take a look here, there are several approaches described in the thread:

Thanks all - I managed to solve it (by chance!) through the following code:

- alias: 'Doors left open'
    hide_entity: true
    trigger:
      platform: state
      entity_id:  binary_sensor.jonathan_presence
      to: 'off'
    condition:
      condition: state
      entity_id: group.doors
      state: 'on'
    action:
      - service: notify.ios_ifone
        data_template:
          message: >
            The following doors have been left open: {{ states | selectattr('entity_id', 'in', state_attr('group.doors','entity_id')) | selectattr('state','in',['on','open']) | list | map(attribute='name') | join(', ') }}

Which generates notifications that look like this:

2 Likes

I know the post is old but can help I have a group I want to see one sensor in open mode out of the whole group and fails Is there a new code?

You can create a dummy “negative” template sensor and to map the states on -> off, and off -> on and then group it with your other entities in the group…