Light on when door opens, light off when door closes

Thank you Glyyn, I am showing my inexperience sorry. when pasting your code into automation.yaml I am getting a lot of duplicated mapping key errors. I have other automations done using the GUI or blueprints, yours will be the first pasted. I have copied my automation.yaml as it is at the moment. anyone know what’s going on.

- id: '1633782070581'
  alias: Let Me know When Front Door Is Open
  description: ''
  trigger:
  - type: opened
    platform: device
    device_id: f287876579fb0f39fda683a78f018f48
    entity_id: binary_sensor.lumi_lumi_sensor_magnet_aq2_on_off
    domain: binary_sensor
    id: on/off
  condition: []
  action:
  - service: notify.mobile_app_fonties_iphone
    data:
      message: Front Door Open
  mode: single
- id: '1633794688075'
  alias: Garage back door open
  description: ''
  trigger:
  - type: opened
    platform: device
    device_id: c770cf2b30dc521b3c1bc15a64c981c4
    entity_id: binary_sensor.lumi_lumi_sensor_magnet_aq2_75f97b06_on_off
    domain: binary_sensor
    id: on/off
  condition: []
  action:
  - service: notify.mobile_app_fonties_iphone
    data:
      message: Garage Back Door Open
  mode: single
- id: '1633795222709'
  alias: Downstairs toilet light on Eugene's Phone
  description: ''
  trigger:
  - platform: device
    type: turned_on
    device_id: 3f17fb5259684b1308e4e3b5455fab33
    entity_id: light.downstairs_toilet_light
    domain: light
    id: on/off
    for:
      hours: 0
      minutes: 5
      seconds: 0
      milliseconds: 0
  condition: []
  action:
  - service: notify.mobile_app_fonties_iphone
    data:
      message: Downstairs Toilet Light Left On
  - service: tts.google_translate_say
    data:
      entity_id: media_player.kitchen_camera
      message: Ok.......Who Left The Downstairs Toilet Light On This Time.........Turn
        It Off Please
  - service: tts.google_translate_say
    data:
      entity_id: media_player.lounge_display
      message: Ok.......Who Left The Downstairs Toilet Light On This Time.........Turn
        It Off Please
  mode: single
- id: '1633863879749'
  alias: Downstairs toilet light on Carol phone
  description: ''
  trigger:
  - platform: device
    type: turned_on
    device_id: 3f17fb5259684b1308e4e3b5455fab33
    entity_id: light.downstairs_toilet_light
    domain: light
    id: on/off
    for:
      hours: 0
      minutes: 5
      seconds: 0
      milliseconds: 0
  condition: []
  action:
  - service: notify.mobile_app_carols_iphone
    data:
      message: Downstairs Toilet Light Left On
  mode: single
- id: '1634339899685'
  alias: Low battery level detection & notification for all battery sensors
  description: ''
  use_blueprint:
    path: sbyx/low-battery-level-detection-notification-for-all-battery-sensors.yaml
    input:
      threshold: 10
      time: '11:00:00'
      actions:
      - service: notify.mobile_app_fonties_iphone
        data:
          message: 'Low battery warning for: {{sensors}}'
      day: 6
      exclude:
        entity_id:
        - sensor.carols_iphone_battery_level
        - sensor.fonties_iphone_battery_level
- id: '1635167216827'
  alias: Lounge Motion and brightness activated Light
  description: ''
  use_blueprint:
    path: OliverHi/motion_brightness_light.yaml
    input:
      motion_entity: binary_sensor.philips_sml001_7ae8090b_on_off
      brightness_entity: sensor.philips_sml001_7ae8090b_illuminance
      light_target:
        entity_id: light.lounge_light_1
      no_motion_wait: 21600
      brightness_trigger: 3
- id: '1635195806093'
  alias: Kitchen Motion and brightness activated Light
  description: ''
  use_blueprint:
    path: OliverHi/motion_brightness_light.yaml
    input:
      motion_entity: binary_sensor.philips_sml001_29ff090b_occupancy
      brightness_entity: sensor.philips_sml001_29ff090b_illuminance
      light_target:
        entity_id:
        - light.cupboard_1
        - light.cupboard_2
        - light.cupboard_3
        - light.kitchen_back_light
      brightness_trigger: 7
      no_motion_wait: 20910


As you are used to using the GUI it should be relatively easy to add the two automations using that method instead of directly adding them to automations.yaml

For the sake of elegaance(!), I have combined these into a single automation.

alias: Toilet door open/closed
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.lumi_lumi_sensor_magnet_aq2_7575a904_on_off
    id: door open
    from: 'off'
    to: 'on'
  - platform: state
    entity_id: binary_sensor.lumi_lumi_sensor_magnet_aq2_7575a904_on_off
    id: door closed
    from: 'on'
    to: 'off'
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: door open
          - condition: state
            entity_id: light.tz3000_dbou1ap4_ts0505a_level_light_color_on_off
            state: 'off'
        sequence:
          - service: light.turn_on
            target:
              entity_id: light.tz3000_dbou1ap4_ts0505a_level_light_color_on_off
      - conditions:
          - condition: trigger
            id: door closed
          - condition: state
            entity_id: light.tz3000_dbou1ap4_ts0505a_level_light_color_on_off
            state: 'on'
            for:
              hours: 0
              minutes: 0
              seconds: 5
              milliseconds: 0
        sequence:
          - service: light.turn_off
            target:
              entity_id: light.tz3000_dbou1ap4_ts0505a_level_light_color_on_off
    default:
      - delay:
          hours: 1
          minutes: 0
          seconds: 0
          milliseconds: 0
      - service: light.turn_off
        target:
          entity_id: light.tz3000_dbou1ap4_ts0505a_level_light_color_on_off
mode: restart

3 Likes

Got it working…Thank you

No problem

Is there a way of setting a timeout when you first open the door, what I am finding is if my wife opens the door to clean or change toilet roll she does this without closing the door so when she is finished the automation thinks there is someone in the toilet and keeps the light on and next person to go into the toilet opens and shuts the door the automation now thinks there is no one in the toilet and turns light off. Hope this makes sense.

Is there a way round this ? Thanks for your help.

See the end of the automation I posted. I put in a delay and a light turn off as a default action just in case the door is left open.

It doesn’t seem to turn the light off…it wife goes in leaves door open to change toilet roll then closes door the light stays on. only thing i changed was the delay which i made 5 minutes.