Sensor and LED Automation

Hi Folks, I’m very knew to Home Assistant. I’ve installed several Smart LEDs in my Bathroom and I have my automation “sort of” working. But im struggling with how to keep the lights on for longer when somebody is in the room.

Im using the IKEA Vallhorn sensors via Zigbee. I’m not sure if I should perhaps be using a scene or an automation to achieve this or if my automation just configured incorrectly.The automation flow is Person enters > If before 11PM > Lights ON (Normal Colour) else > after 11PM lights turn ON (Red)

alias: Automation - Bathroom Lights ON
description: ""
trigger:
  # Trigger when motion is detected (sensor state changes to 'on')
  - platform: state
    entity_id: binary_sensor.sensor_bathroom_occupancy
    to: "on"
  # Trigger when no motion is detected (sensor state changes to 'off')
  - platform: state
    entity_id: binary_sensor.sensor_bathroom_occupancy
    to: "off"
conditions:
  - condition: numeric_state
    entity_id: sensor.sensor_bathroom_illuminance
    below: 10
action:
  - choose:
      # Turn on lights if motion is detected
      - conditions:
          - condition: state
            entity_id: binary_sensor.sensor_bathroom_occupancy
            state: "on"
        sequence:
          - choose:
              - conditions:
                  - condition: time
                    before: "23:00:00"
                sequence:
                  - service: light.turn_on
                    target:
                      entity_id: light.bathroom_lights
                    data:
                      kelvin: 2000
                      brightness_pct: 44
              - conditions:
                  - condition: time
                    after: "23:00:00"
                sequence:
                  - service: light.turn_on
                    target:
                      entity_id: light.bathroom_lights
                    data:
                      color_name: red
                      brightness_pct: 50
      # Turn off lights if no motion is detected
      - conditions:
          - condition: state
            entity_id: binary_sensor.sensor_bathroom_occupancy
            state: "off"
        sequence:
          - service: light.turn_off
            target:
              entity_id: light.bathroom_lights
mode: restart

What method are you using to determine that the bathroom is occupied?

FWIW, you should probably include both before and after times for your conditions, otherwise the “red” action will only work from 23:00-23:59.

- condition: time
  after: "23:00:00"
  before: "07:00:00"

Appologies, Ive updated the original code to what it should have been!

Essentially im using the bathroom occupancy state. But the automation doesnt stay “live” long enough to keep checking for that state, sorry if my terminology is incorrect!

The original post used a trigger based on a door sensor… is there a door sensor as well as the motion sensor? If so, you might want to consider:

No its just one motion sensor.

The downside with PIR motion sensors is if you sit still or stand still too long. The timer will go clear because it doesn’t do good with static presence. So what you can do is if you add the for: to your off command for say like 5 minutes. It will wait to ensure that there is no motion for 5 minutes before turning it off.

Also a advanced way to handle this would be when motion is cleared start a timer and when the timer finishes check to see if there’s motion and if there is motion then restart the timer. If there is no motion, turn off the light.

A second and already mentioned method would be adding a door sensor. That way you assume that the room is occupied. If the door is closed. That is the wasp in the box blueprint mentioned earlier does. Downside with that is there’s no way to tell the directionality of the door meaning was the door closed by entering or was the door closed by someone just passing by so the light will stay on. Basically assumes the normal date of that room is the door is open when unoccupied.