sensor:
  - platform: mqtt
    name: "Bedside switch battery"
    state_topic: "z2m_deconz/bedside_switch"
    availability_topic: "z2m_deconz/bridge/state"
    unit_of_measurement: "%"
    device_class: "battery"
    value_template: "{{ value_json.battery }}"
  - platform: mqtt
    name: "Bedside switch LQI"
    state_topic: "z2m_deconz/bedside_switch"
    availability_topic: "z2m_deconz/bridge/state"
    icon: "mdi:signal"
    unit_of_measurement: "lqi"
    value_template: "{{ value_json.linkquality }}"

automation:
  - alias: Bedroom · Bedside Switch
    use_blueprint:
      path: aqara_switch.yaml
      input:
        z2m_topic: "z2m_deconz/bedside_switch"
        click:
          - service: script.bed_led_toggle
            data_template:
              brightness_pct: >-
                {% if is_state('input_select.house_mode', 'night') %} 40 {% else %} 100 {% endif %}

          - service: light.toggle
            data_template:
              entity_id: >-

                {% if is_state('input_select.house_mode', 'day') %}
                  light.master_bedroom_main
                {% elif is_state('input_select.house_mode', 'bed') %}
                  light.bedside
                {% endif %}
              brightness_pct: >-
                {% if is_state('input_select.house_mode', 'night') %} 40 {% else %} 100 {% endif %}

        double_click:
          - service: light.toggle
            entity_id: light.bedside
            data:
              brightness_pct: 100
          - service: script.bed_led_toggle
            data_template:
              brightness_pct: >-
                {% if is_state('input_select.house_mode', 'night') %} 40 {% else %} 100 {% endif %}

        hold:
          - service: input_select.select_next
            entity_id: input_select.house_mode
