How to keep the light on?

I’m fairly new to HA and happy that I have all my devices working. But I ran into a problem which I cannot solve. I’m using a contact sensor for my toilet. So, open door, light on, but when closing the door, the light obviously turns off. So somebody hinted about a boolean toggle, which should keep the light on while the toilet is occupied and turn off the light when the door is opened and closed again.

So how do I use this toggle and where to call it in my automation? Thanks for your help!

You could do it really simply with a timer to wait 10 mins and then turn off light when door shut.

I know, you should be done in 10 minutes, but you know kids and Youtube, right?
And I’d like to practice with this so I can use it in other automations. Still in learning mode.

Be careful on using only the door sensor to define if someome are inside or not, as one can open the door and then close it without entering the room or even use the room with the doors opened, and then when he/she leaves the light will say on until the door is opened again, inverting the desired behaviour and increasing your electricity bill.

With that said, you can do what you say in a much simpler way by using only the “door opening” to trigger a light.toggle, don’t use the “door closed”. It will be super simple, but the light will turn off right away when you open the door to leave (which might not be desired), so perhaps you can add a couple of seconds as delay before turning off the lights.

Easy way:

description: ""
mode: single
trigger:
  - type: opened
    platform: device
    device_id: a2e059c66daa6661b4793e090e03c994
    entity_id: binary_sensor.bathroom_door
    domain: binary_sensor
condition: []
action:
  - type: toggle
    device_id: 3cdd74bfe34c054238c9d0f07cccffb5
    entity_id: light.bathroom_ceiling_lights
    domain: light

Or a little less simple:

description: ""
mode: single
trigger:
  - platform: state
    entity_id:
      - binary_sensor.bathroom_door
    from: "off"
    to: "on"
condition: []
action:
  - if:
      - condition: state
        entity_id: light.bathroom_ceiling_lights
        state: "on"
    then:
      - wait_for_trigger:
          - platform: state
            entity_id:
              - binary_sensor.bathroom_door
            from: "on"
            to: "off"
        timeout:
          hours: 0
          minutes: 0
          seconds: 5
          milliseconds: 0
  - service: light.toggle
    data: {}
    target:
      entity_id: light.bathroom_ceiling_lights

But if you really want to use a bool toggle, you can create one under Settings > Devices & Services > Helpers (tab), then in your automation you select to call a service and use the service input_boolean.toggle.

actually it’s completely wrong approach (you figure it ou later on your own).

The opening or closing a toiled door cannot be used to decide someone is in or out.
It works for small rooms you cannot be closed in (small storages, pantry etc).
Toilets, if you expect automatic lights turn on/off must be supported with motion sensors (better if they recognize the presence rather than movement).

This advise is valid for all automations you will think of:

  1. don’t automate for sake of automation alone
  2. automation has to help you, without adding unwanted corner cases

Personally because I have no motion sensor, I left toggling to power switch. the only think I added is timer the light turns off after reasonable amount of some time)

1 Like

Thanks for all the help. I forgot to tell you that I’m using a Hue light so that’s the one I’m switching. I understand the part about switching on.

description: ''
  trigger:
  - platform: state
    entity_id:
    - binary_sensor.contact_wc_contact
    from: 'off'
    to: 'on'
    for:
      hours: 0
      minutes: 0
      seconds: 0
  condition: []
  action:
  - if:
    - condition: state
      entity_id: light.hue_ambiance_lamp_1_2
      state: 'on'
    then:
    - wait_for_trigger:
      - platform: state
        entity_id:
        - binary_sensor.contact_wc_contact
        from: 'on'
        to: 'off'
        for:
          hours: 0
          minutes: 0
          seconds: 0
  mode: single

But nothing happens now…

Try this:

description: ""
mode: single
trigger:
  - platform: state
    entity_id:
      - binary_sensor.contact_wc_contact
    from: "off"
    to: "on"
condition: []
action:
  - if:
      - condition: state
        entity_id: light.hue_ambiance_lamp_1_2
        state: "on"
    then:
      - wait_for_trigger:
          - platform: state
            entity_id:
              - binary_sensor.contact_wc_contact
            from: "on"
            to: "off"
        timeout:
          hours: 0
          minutes: 0
          seconds: 5
          milliseconds: 0
  - service: light.toggle
    data: {}
    target:
      entity_id: light.hue_ambiance_lamp_1_2

I fully agree with @maxym, a door sensor only is not ideal for a room where you stay inside. Your automation will work, but you will easily have unexpected behaviour (or the reasons I mentioned before) and you will get frustrated…
Think about adding a motion sensor to your set. If a motion happens after the door is closed, you have someone inside, so the light should be on until the door opens, regardless is no more movement is detected.

If you only ever have the door closed when occupied you could invert the logic (door open->light off, door closed → light on).

otherwise there is no way you can be sure to keep everything in synch with just the door sensor as already noted.

you could add a motion sensor inside to add another data point to keep the lights on when there is movement. But even then if you are still longer than the time setting the lights might still turn off but if the time setting is reasonable then that is less likely an issue than just relying on the door sensor only.

And if you leave the toilet closing the door to avoid stink, you left light turned on?

I use this automation:
Door opens, light on
Door closes within 10 seconds, light stays on
Door opens, light stays on
Door closes, light off
or
Door opens, light on
Door close after 10 seconds, light off.

- alias: WC licht
  description: 'Licht aan als de deur open gaat, licht uit als de deur sluit'
  mode: restart

  trigger:
    - platform: state
      entity_id: binary_sensor.toiletdeur_contact
      to:  # a null 'to' only triggers on all state changes, not attribute changes
    - platform: time
      at: input_datetime.delayed_off

  action:
  - variables:
      light: light.wc_lamp
      is_day: '{{ is_state("sun.sun", "above_horizon") or is_state("switch.gang_1", "on") }}'

  - choose:
  # Turn off the light when 25 minutes have passed, or the door is closed after 10 seconds
    - conditions:
        - or:
            - '{{ trigger.platform == "time" }}'
            - and:
                - '{{ trigger.to_state.state == "off" }}'
                - condition: state
                  entity_id: light.wc_lamp
                  state: 'on'
                  for: 10
      sequence:
        - service: light.turn_off
          target:
            entity_id: '{{ light }}'

  # Turn on the light when the door is opened and the light is off
    - conditions:
        - '{{ trigger.to_state.state == "on" }}'
        - '{{ is_state(light, "off") }}'
      sequence:
        - service: light.turn_on
          data:
            brightness_pct: '{{ 70 if is_day else 10 }}'
          target:
            entity_id: '{{ light }}'

  # Set the timer for 25 minutes after the door is closed and the light is on
    default:
      - condition:
          - '{{ trigger.to_state.state == "off" }}'
          - '{{ is_state(light, "on") }}'
      - service: input_datetime.set_datetime
        target:
          entity_id: input_datetime.delayed_off
        data:
          timestamp: '{{ (now() + timedelta(minutes=25)) | as_timestamp }}'
2 Likes

that’s why I said “if you…”.

not everyone closes the door when unoccupied. We don’t in our house.

Use a motion sensor.

I have been trying to program this for a while but couldn’t this exact solution. I have a Kasa motion sensor with dimmer and aqara door sensors. The light always turns off after no motion is detected even when the door is closed.

Share your automation so we can suggest something. :wink:

Or you can use this Blueprint ime currently using. There are other inputs there, but you can ignore most of those.

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

Thanks for coming back on this so promptly!

I imported the blueprint but my bathroom door sensor and light dropdowns, both were empty. So I went to edit the code in YAML and just pasted the entity ID on each manually. FYI, I have a Kasa Motion Sensor with Dimmer installed and the Aqara door sensors. Almost all the time, the bathroom door is open and obviously only close when in use.

I am not sure if you have used Kasa products or sensors before. But on the Kasa app, I have the default smart control setting on it for now, which comes preprogrammed. Which are 12 am to 12 am and triggers light on when motion & dark and when no motion is detected for 15 seconds, it turns off the light.

Giving you details, so you can help! Cause My light still turns off after 15 seconds of closing the door and no motion.

Here is the code

alias: Entryway Toilet - Test - Soution EdwardTFN
description: ""
use_blueprint:
  path: edwardtfn/light_motion_illuminance_2_levels.yaml
  input:
    motion_entity: switch.guest_toilet_motion
    bathroom_door: binary_sensor.contact_sensor_2
    light_target: switch.guest_toilet_motion
    no_motion_wait_to_dimming: 20
    no_motion_wait_to_off: 25
    light_level_dimmer: 50

You have to disable the automation on Kasa app, so your lights will only be controlled by Home Assistant. If you have two automations it could be conflicting.

Thanks Edwards, apparently its a Kasa issue and nothing wrong with the blueprint. Tp link ks220m smart swich with motion and light sensors support - #42 by starsoccer

Apparently motion entities are yet recognized in HA for these switches