######################################################################
##  Good night Routine
######################################################################

- alias: 'Good Night Trigger'
  id: 26846f7b-bc76-43d2-99be-af552da7700f
  trigger:
    - platform: state
      entity_id: input_boolean.good_night
      to: 'on'
      from: 'off'
    - platform: state
      entity_id:
        - binary_sensor.sleepnumber_carlo_carlo_is_in_bed
        - binary_sensor.sleepnumber_carlo_stacey_is_in_bed
      to: 'on'

  condition:
    - condition: state
      entity_id: sun.sun
      state: 'below_horizon'
    - condition: time
      after: "15:00:00"
      before: "04:00:00"
    - condition: state
      entity_id: input_boolean.guest_mode
      state: 'off'

    - condition: or
      conditions:
        - condition: state
          entity_id: binary_sensor.sleepnumber_carlo_carlo_is_in_bed
          state: 'on'
        - condition: state
          entity_id: person.carlo
          state: 'not_home'
    - condition: or
      conditions:
        - condition: state
          entity_id: binary_sensor.sleepnumber_carlo_stacey_is_in_bed
          state: 'on'
        - condition: state
          entity_id: person.stacey
          state: 'not_home'

  action:
    - service: input_boolean.turn_off
      entity_id: input_boolean.good_night
    - service: homeassistant.turn_off
      entity_id: group.outdoor_seasonal
    - service: homeassistant.turn_off
      entity_id: switch.kitchen_accent_2
    - service: homeassistant.turn_off
      entity_id: group.master_bathroom_accents
    
    - service: script.interior_off

    - wait_template: "{{ states.media_player.master_bedroom.state == 'standby' }}"
      timeout: '00:30:00'

    - service: homeassistant.turn_off
      entity_id: group.landscaping

- alias: 'Bed Presence AMP Trigger'
  id: 26846f7b-bc76-43d2-99be-af552d2300f
  trigger:
    - platform: state
      entity_id:
        - binary_sensor.sleepnumber_carlo_carlo_is_in_bed
        - binary_sensor.sleepnumber_carlo_stacey_is_in_bed
      to: 'on'
      id: "in_bed"
    - platform: state
      entity_id: group.bed
      to: 'off'
      id: "out_of_bed"

  action:
    choose:
      - alias: "SomeOne in Bed"
        conditions:
          - condition: trigger
            id: in_bed
        sequence:
          - service: homeassistant.turn_off
            target:
              entity_id: switch.lr_amp
          - service: homeassistant.turn_off
            target:
              entity_id: 
                - switch.kitchen_accent_2
                - group.master_bathroom_accents

    default:
      - service: homeassistant.turn_on
        target:
          entity_id: switch.lr_amp