group:
  all_motion_sensors:
    name: All motion_sensors
    entities:
      - binary_sensor.hallway_motion_occupancy
      - binary_sensor.kitchen_motion_occupancy
      - binary_sensor.multisensor_motion
      - binary_sensor.trisensor_motion

  hallway_motion:
    name: Hallway Motion
    entities:
      - binary_sensor.hallway_motion_occupancy
      - binary_sensor.trisensor_motion

binary_sensor:
  - platform: mqtt
    name: "Hallway motion occupancy"
    state_topic: "z2m_deconz/hallway_motion"
    availability_topic: "z2m_deconz/bridge/state"
    payload_on: true
    payload_off: false
    value_template: "{{ value_json.occupancy }}"
    device_class: "motion"
  - platform: mqtt
    name: "Kitchen motion occupancy"
    state_topic: "z2m_deconz/kitchen_motion"
    availability_topic: "z2m_deconz/bridge/state"
    payload_on: true
    payload_off: false
    value_template: "{{ value_json.occupancy }}"
    device_class: "motion"

sensor:
  # Hallway Motion
  - platform: mqtt
    name: "Hallway motion illuminance"
    state_topic: "z2m_deconz/hallway_motion"
    availability_topic: "z2m_deconz/bridge/state"
    device_class: "illuminance"
    value_template: "{{ value_json.illuminance_lux }}"
  - platform: mqtt
    name: "Hallway motion battery"
    state_topic: "z2m_deconz/hallway_motion"
    availability_topic: "z2m_deconz/bridge/state"
    unit_of_measurement: "%"
    device_class: "battery"
    value_template: "{{ value_json.battery }}"
  - platform: mqtt
    name: "Hallway motion LQI"
    state_topic: "z2m_deconz/hallway_motion"
    availability_topic: "z2m_deconz/bridge/state"
    icon: "mdi:signal"
    unit_of_measurement: "lqi"
    value_template: "{{ value_json.linkquality }}"

  # Kitchen Motion
  - platform: mqtt
    name: "Kitchen motion illuminance"
    state_topic: "z2m_deconz/kitchen_motion"
    availability_topic: "z2m_deconz/bridge/state"
    device_class: "illuminance"
    value_template: "{{ value_json.illuminance_lux }}"
  - platform: mqtt
    name: "Kitchen motion battery"
    state_topic: "z2m_deconz/kitchen_motion"
    availability_topic: "z2m_deconz/bridge/state"
    unit_of_measurement: "%"
    device_class: "battery"
    value_template: "{{ value_json.battery }}"
  - platform: mqtt
    name: "Kitchen motion LQI"
    state_topic: "z2m_deconz/kitchen_motion"
    availability_topic: "z2m_deconz/bridge/state"
    icon: "mdi:signal"
    unit_of_measurement: "lqi"
    value_template: "{{ value_json.linkquality }}"

  - platform: template
    sensors:
      active_motion_sensors:
        friendly_name: Active motion sensor count
        value_template: >-
          {% set entityStates = states | selectattr('entity_id', 'in', state_attr('group.all_motion_sensors', 'entity_id')) %}
          {{ entityStates | selectattr('state', 'eq', 'on') | list | count | int }}
