input_boolean:
  sleep_mode:
    name: Sleep mode
    initial: off
    icon: mdi:sleep

automation:
  - alias: Sleep mode · Enable
    trigger:
      platform: state
      entity_id: input_boolean.sleep_mode
      from: "off"
      to: "on"
    action:
      - service: script.routine_good_night

  - alias: Sleep mode · Disable
    trigger:
      platform: state
      entity_id: input_boolean.sleep_mode
      from: "on"
      to: "off"
    action:
      - service: script.routine_waking_up

  - alias: Sleep mode · Toggle on bedside switch double click
    trigger:
      - platform: event
        event_type: zha_event
        event_data:
          device_ieee: !secret master_bedroom_bedside_switch_ieee
          command: double
    action:
      - service_template: >-
          {% if now().strftime('%H')|int > 20 or now().strftime('%H')|int < 6 %}
            input_boolean.turn_on
          {% else %}
            input_boolean.turn_off
          {% endif %}
        entity_id: input_boolean.sleep_mode
