I have an automation that warns if any of our external doors are opened while the whole family is away. This is my current automation:
- id: '1672842695017'
alias: Warning - External door opened when Family is away
description: ''
triggers:
- entity_id:
- group.external_doors
to: 'on'
trigger: state
conditions:
- condition: and
conditions:
- condition: state
entity_id: group.family
state: not_home
- condition: not
conditions:
- condition: state
entity_id: input_select.houskeeping_status
state: Active
actions:
- choose:
- conditions:
- condition: and
conditions:
- condition: state
entity_id: group.family
state: not_home
- condition: not
conditions:
- condition: state
entity_id: input_select.houskeeping_status
state: Active
sequence:
- data:
message: '{% set doors = expand(''group.external_doors'') | selectattr(''state'',
''eq'', ''on'') | map(attribute=''name'') | list %} {% set qty = doors
| count %} {% set p1 = ''has been'' if qty > 1 else ''have been'' %} {%
if qty == 0 %} No doors are open. {% else %} Warning: {{'' and ''.join((doors
| join('', '')).rsplit('', '', 1))}} {{p1}} opened and only Queenie seems
to be at home. {% endif %}'
title: 'Warning: External door(s) opened!'
action: notify.mobile_app_christer_s_s23_ultra
- data:
title: 'Warning: External door(s) opened!'
data:
sound: climb
priority: 1
message: '{% set doors = expand(''group.external_doors'') | selectattr(''state'',
''eq'', ''on'') | map(attribute=''name'') | list %} {% set qty = doors
| count %} {% set p1 = ''has been'' if qty > 1 else ''have been'' %} {%
if qty == 0 %} No doors are open. {% else %} Warning: {{'' and ''.join((doors
| join('', '')).rsplit('', '', 1))}} {{p1}} opened and only Queenie seems
to be at home. {% endif %}'
action: notify.pushover
- data:
message: '{% set doors = expand(''group.external_doors'') | selectattr(''state'',
''eq'', ''on'') | map(attribute=''name'') | list %} {% set qty = doors
| count %} {% set p1 = ''has been'' if qty > 1 else ''have been'' %} {%
if qty == 0 %} No doors are open. {% else %} Warning: {{'' and ''.join((doors
| join('', '')).rsplit('', '', 1))}} {{p1}} opened and only Queenie seems
to be at home. {% endif %}'
action: notify.whatsapp
mode: single
This works great, except that some times my home/away sensor(s) only change to ‘home’ a few seconds after our front door has been opened, leading to false notifications. So I tried to rewrite the automation with a 2 minute delay between the door being opened and the actual notification. This is my attempt:
- id: '1672842695017'
alias: Warning - External door opened when Family is away
description: ''
triggers:
- entity_id:
- group.external_doors
to: 'on'
trigger: state
conditions:
- condition: and
conditions:
- condition: state
entity_id: group.family
state: not_home
- condition: not
conditions:
- condition: state
entity_id: input_select.houskeeping_status
state: Active
action:
- service: variable.set_variable #line 3211
data:
variable: opened_doors_list
value: >
{% set doors = expand('group.external_doors') | selectattr('state', 'eq', 'on') %}
{% set door_names = [] %}
{% for door in doors %}
{% set door_name = state_attr(door.entity_id, 'friendly_name') or door.entity_id | replace('binary_sensor.', '') | replace('_', ' ') | title %}
{% set _ = door_names.append(door_name) %}
{% endfor %}
{{ door_names | to_json }}
- delay:
minutes: 2
- choose: # line 3226
- conditions:
- condition: and
conditions:
- condition: state
entity_id: group.family
state: not_home
- condition: not
conditions:
- condition: state
entity_id: input_select.houskeeping_status
state: Active
sequence:
- service: notify.mobile_app_christer_s_s23_ultra
data:
title: "Warning: External door(s) opened while away!"
message: >
{% set opened_doors = states('variable.opened_doors_list') | from_json %}
{% if opened_doors | count > 1 %}
{{ opened_doors[:-1] | join(', ') }} and {{ opened_doors[-1] }} have been opened and only Queenie seems to be at home.
{% elif opened_doors | count == 1 %}
{{ opened_doors }} has been opened and only Queenie seems to be at home.
{% else %}
No doors were opened while you were away. Check sensors!
{% endif %}
- service: notify.pushover
data:
title: 'Warning: External door(s) opened while away!'
message: >
{% set opened_doors = states('variable.opened_doors_list') | from_json %}
{% if opened_doors | count > 1 %}
{{ opened_doors[:-1] | join(', ') }} and {{ opened_doors[-1] }} have been opened and only Queenie seems to be at home.
{% elif opened_doors | count == 1 %}
{{ opened_doors }} has been opened and only Queenie seems to be at home.
{% else %}
No doors were opened while you were away. Check sensors!
{% endif %}
data:
sound: climb
priority: 1
- service: notify.whatsapp
data:
message: >
{% set opened_doors = states('variable.opened_doors_list') | from_json %}
{% if opened_doors | count > 1 %}
{{ opened_doors[:-1] | join(', ') }} and {{ opened_doors[-1] }} have been opened and only Queenie seems to be at home.
{% elif opened_doors | count == 1 %}
{{ opened_doors }} has been opened and only Queenie seems to be at home.
{% else %}
No doors were opened while you were away. Check sensors!
{% endif %}
mode: single
What is not working here is the " - service: variable.set_variable" part, and I cannot figure out what I’m doing wrong here. I’m attaching the error messages I see in the Lovelace UI, and I’ve made comments in the above automation st show the line number that is referenced in the attached screenshot.
Maybe someone more skilled in yaml/jinja2 than myself can see what I’m doing wrong here?