Notification if door or windows are open

I have a working notification that sends a telegram-message about which doors or windows are open.
But how can I tweak this to only send the message IF something actually is open?

As an example, this is what it sends now:
If nothings is open: The following doors and windows are open: “”
Is something is open: The following doors and windows are open “Front door, bathroom window”

message: >-
  Følgande dører og vinduer er opne! "{{ states | selectattr('entity_id', 'in',
  state_attr('group.all_doors_and_windows','entity_id')) |
  selectattr('state','in',['on','open']) | list | map(attribute='name') |
  join(', ') }}"

How are you triggering your automation? It shouldn’t trigger when no door/window is open
. Please show the full automation.

We use booleans to turn on or off different “modes” or “scenes” in our house. Like “Home” or “God night” or “Gone”.

alias: Varsling dersom vinduer eller dører åpne
description: ''
trigger:
  - entity_id: input_boolean.godnatt
    platform: state
    to: 'on'
  - entity_id: input_boolean.kjorer_hjemmefra
    platform: state
    to: 'on'
  - entity_id: input_boolean.gaar_hjemmefra
    platform: state
    to: 'on'
  - entity_id: input_boolean.ferie
    platform: state
    to: 'on'
  - platform: state
    entity_id: input_boolean.kveldskos
    to: 'on'
condition: []
action:
  - data:
      message: >-
        Følgande dører og vinduer er opne! "{{ states | selectattr('entity_id',
        'in', state_attr('group.all_doors_and_windows','entity_id')) |
        selectattr('state','in',['on','open']) | list | map(attribute='name') |
        join(', ') }}"
    service: notify.telegram_daniel
  - service: notify.telegram_anette
    data:
      message: >-
        Følgande dører og vinduer er opne! "{{ states | selectattr('entity_id',
        'in', state_attr('group.all_doors_and_windows','entity_id')) |
        selectattr('state','in',['on','open']) | list | map(attribute='name') |
        join(', ') }}"
mode: single

What kind of entities are in this group.all_doors_and_windows? Only binary_sensors or also sensors?

Only binary

  all_doors_and_windows:
    name: Dører og vinduer
    icon: mdi:window-open-variant
    entities:
      - binary_sensor.inngangsdor
      - binary_sensor.terrassedor
      - binary_sensor.loftstuedor
      - binary_sensor.garasjedor
      - binary_sensor.dorinnigarasjen
      - binary_sensor.kjoleskapssensor
      - binary_sensor.vindu_vart_soverom
      - binary_sensor.malinavindu
      - binary_sensor.vindu_bad_oppe
      - binary_sensor.kjokkenvindu_hogre
      - binary_sensor.kjokkenvindu_venstre
      - binary_sensor.kjokkenvindu_skulen

Ok, then try this:

alias: Varsling dersom vinduer eller dører åpne
description: ''
trigger:
  - entity_id: 
      - input_boolean.godnatt
      - input_boolean.kjorer_hjemmefra
      - input_boolean.gaar_hjemmefra
      - input_boolean.ferie
      - input_boolean.kveldskos
    platform: state
    to: 'on'
condition:
  condition: state
  entity_id: group.all_doors_and_windows
  state: 'on'
action:
  - variables:
      open_doors: "{{ expand('group.all_doors_and_windows')|selectattr('state', 'eq', 'on')|map(attribute='name')|list }}"
  - service: notify.telegram_daniel
    data:
      message: >-
        Følgande dører og vinduer er opne! {{ open_doors }}
  - service: notify.telegram_anette
    data:
      message: >-
        Følgande dører og vinduer er opne! {{ open_doors }}
mode: single

If at least one window/door is open (at least one binary_sensor is on) then the group state will be ‘on’. So you can use the group state as a condition. I also tried to simplify/shorten your code.

1 Like

Thank you so much for your time!

I get:
“Message malformed: invalid template (TemplateSyntaxError: expected token ‘end of print statement’, got ‘doors’) for dictionary value @ data[‘action’][2][‘data’]”

When trying to save it. Any pointers?
I have simply pasted it as a YAML-edit.

You solved my issue with something as simple as the condition.

I added it like this:

alias: Varsling dersom vinduer eller dører åpne
description: ''
trigger:
  - entity_id: input_boolean.godnatt
    platform: state
    to: 'on'
  - entity_id: input_boolean.kjorer_hjemmefra
    platform: state
    to: 'on'
  - entity_id: input_boolean.gaar_hjemmefra
    platform: state
    to: 'on'
  - entity_id: input_boolean.ferie
    platform: state
    to: 'on'
  - platform: state
    entity_id: input_boolean.kveldskos
    to: 'on'
condition:
  - condition: state
    entity_id: group.all_doors_and_windows
    state: 'on'
action:
  - data:
      message: >-
        Følgande dører og vinduer er opne! "{{ states | selectattr('entity_id',
        'in', state_attr('group.all_doors_and_windows','entity_id')) |
        selectattr('state','in',['on','open']) | list | map(attribute='name') |
        join(', ') }}"
    service: notify.telegram_daniel
  - service: notify.telegram_anette
    data:
      message: >-
        Følgande dører og vinduer er opne! "{{ states | selectattr('entity_id',
        'in', state_attr('group.all_doors_and_windows','entity_id')) |
        selectattr('state','in',['on','open']) | list | map(attribute='name') |
        join(', ') }}"
mode: single

But if your willing to use more time with your simplified / shortened code, I am interested !

The second message misses a _ between open and doors. I fixed it.

Thank you very much!
That did the trick!

1 Like