Doors open?

Hi! I have three doors in our home and they all have a magnet sensor. I want to get information in a markdown card which door is open. And if no doors is open it will write out “no doors open”.

I have all doors in a group and I want to select the door that has status on and write it out. And if a second door is open this door will also write out as open.

Exempel - one door open
“Door 1 is open”

Exempel - two doors open
“Door 1 and 2 is open”

Exempel - three doors open
“Door 1,2 and 3 is open”

Exempel - no doors open
“No doors is open”

Can anyone help me?

I do something similar with the auto entities card.
I just don’t have the card show when there is nothing open.

type: 'custom:auto-entities'
sort:
  method: state
  numeric: true
card:
  type: 'custom:bar-card'
  color: '#343aeb'
  show_header_toggle: false
  title: ''
show_empty: false
unique: true
filter:
  include:
    - entity_id: '*Door*'
    - entity_id: '*Window*'
  exclude:
    - state: 'off'
    - state: closed
    - domain: automation
    - state: unavailable
    - entity_id: '*trap*'
    - entity_id: '*control*'
    - entity_id: '*battery*'
    - entity_id: '*burg*'
    - state: '*sleeping*'
    - entity_id: '*id*'
    - entity_id: '*alarm*'
    - entity_id: '*ping*'
    - entity_id: '*gateway*'
    - state: initializing
    - state: =0
    - state: dead

Is there a reason for specifically a markdown card?

On a separate card I list last state change of my doors and windows:

type: vertical-stack
cards:
  - type: 'custom:auto-entities'
    sort:
      method: entity_id
      numeric: true
    card:
      type: entities
      title: Door & Window
      state_color: true
    show_empty: false
    unique: true
    filter:
      include:
        - name: '*Door*'
          options:
            secondary_info: last-changed
        - name: '*Window*'
          options:
            secondary_info: last-changed
      exclude:
        - state: unknown
        - domain: automation
        - entity_id: '*doorbell*'
        - name: '*Trap*'
        - name: '*Battery*'
        - entity_id: '*myq*'
        - entity_id: '*ping*'
  - type: history-graph
    entities:
      - entity: binary_sensor.deep_freezer_door
      - entity: binary_sensor.fridge_door
      - entity: binary_sensor.front_door
      - entity: binary_sensor.kitchen_window
      - entity: binary_sensor.sliding_glass_door
      - entity: cover.garage_door_big
      - entity: cover.garage_door_small
    hours_to_show: 24
    refresh_interval: 0

This template will do what you want. Simply replace the list of binary_sensors with your own.

{% set doors = expand('binary_sensor.front_door', 'binary_sensor.rear_door', 'binary_sensor.garage_door')
        | selectattr('state', 'eq', 'on')
        | map(attribute='name') | list %}
{% set qty = doors | count %}
{% set p1 = 'are' if qty > 1 else 'is' %}

{% if qty == 0 %} 
 No doors are open.
{% else %}
 {{' and '.join((doors | join(', ')).rsplit(', ', 1))}} {{p1}} open.
{% endif %}

The template uses this technique in order to use the word “and” correctly when there are two or more open doors.

The template can be used in a Markdown card or even in a Template Sensor.

If you wish to experiment with the template, simply paste it into the Template Editor.

6 Likes

Exactly. Is it possible to get the first sensor to have capital? The first sensor in the code will be placed first, but if this door isn’t open can the first sensor have a capital letter?

The template uses the binary_sensor’s friendly_name. If it’s “Front Door” then that’s how it will appear. Ensure your three binary_sensors all have a friendly_name (with a capitalized first letter)

If the binary_sensor doesn’t have a friendly_name then the template will use its object_id. For example, if it’s binary_sensor.rear_door, it will appear as “rear_door”.

Hi , may I ask how you managed to have your entities displayed with the device class for “Open/Closed” for the “door and window closed state” card? I’m stuck at it showing the On state.

You need to set device_class to either door or window.
If you set it up in yaml just add device_class, if it is in through an integration then you might have to use customize.yaml to change/add the device class.

1 Like

Hi I tried the above method with my 4 binary sensors but get the following error -

Configuration errors detected:
missed comma between flow collection entries (3:2)

 1 | Card type: Markdown 
 2 | content:
 3 | {% set doors = expand('binary_sen ...
------^
 4 |       | selectattr('state', 'eq', ...
 5 |       | map(attribute='name') | l ...

Any reason what would cause this? It works perfectly fine when I try it in the template view.

Thanks in advance.

Are you certain that what you tested in the Template Editor is identical to what you have in the Markdown card? The error message indicates:

missed comma between flow collection entries

Post the code you have in the Markdown card (and/or a screenshot of it).