group:
  all_doors:
    name: All doors
    entities:
      - binary_sensor.front_door
      - binary_sensor.bedroom_door

sensor:
  - platform: mqtt
    name: "Bedroom door battery"
    state_topic: "z2m_deconz/bedroom_door"
    availability_topic: "z2m_deconz/bridge/state"
    unit_of_measurement: "%"
    device_class: "battery"
    value_template: "{{ value_json.battery }}"

  - platform: mqtt
    name: "Bedroom door LQI"
    state_topic: "z2m_deconz/bedroom_door"
    availability_topic: "z2m_deconz/bridge/state"
    icon: "mdi:signal"
    unit_of_measurement: "lqi"
    value_template: "{{ value_json.linkquality }}"

  - platform: mqtt
    name: "Front door battery"
    state_topic: "z2m_deconz/front_door"
    availability_topic: "z2m_deconz/bridge/state"
    unit_of_measurement: "%"
    device_class: "battery"
    value_template: "{{ value_json.battery }}"

  - platform: mqtt
    name: "Front door LQI"
    state_topic: "z2m_deconz/front_door"
    availability_topic: "z2m_deconz/bridge/state"
    icon: "mdi:signal"
    unit_of_measurement: "lqi"
    value_template: "{{ value_json.linkquality }}"

  - platform: template
    sensors:
      open_door_count:
        friendly_name: Open door count
        value_template: >-
          {% set entityStates = states | selectattr('entity_id', 'in', state_attr('group.all_doors', 'entity_id')) %}
          {{ entityStates | selectattr('state', 'eq', 'on') | list | count | int }}


binary_sensor:
  - platform: mqtt
    name: "Bedroom door"
    state_topic: "z2m_deconz/bedroom_door"
    availability_topic: "z2m_deconz/bridge/state"
    payload_on: false
    payload_off: true
    value_template: "{{ value_json.contact }}"
    device_class: "door"
  - platform: mqtt
    name: "Front door"
    state_topic: "z2m_deconz/front_door"
    availability_topic: "z2m_deconz/bridge/state"
    payload_on: false
    payload_off: true
    value_template: "{{ value_json.contact }}"
    device_class: "door"
