Beginner - Turn on lights with motion sensor at different levels according to time of day

Hi all, I’m quite new to automation in HA. I have some basic automations, but I need someone to point me in the right direction. I am moving from Apple Home and would like to mimic the previous behavior. I have a couple of motion sensors that trigger different lighting conditions depending on the time of day.

  • Ensuite:
    • Between 6am - 9:45pm, if motion is detected turn on the walkin robe and ensuite lights at 100%. After 10 min turn the light off, restart the timer if motion is detected again.
    • Between 9:45pm and 6am, if motion is detected turn on the walkin robe and ensuite lights at 10%. Turn the light off after 5min.
  • Hallway - same as above with different lights and times.

I’m not really sure where to start? How do I trigger based on time and motion?

If I were to do this automation for myself, I would do it based on the elevation of the sun or outdoor illuminance if you have a sensor that provides that. Different times of the year, different brightness outside.

For a similar effect in my own home I actually use the adaptive lighting custom integration.

Anyway, to do what you asked for, here is the YAML created from the UI. Obviously change the entities to your own entities. You should be able to create a new automation, edit the YAML, and paste this there.

TOD Light Brightness Automation
alias: TOD Office Motion
description: Turn on the office lights with brightness determined by the time of day.
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.office_motion
    to: "on"
  - trigger: state
    entity_id:
      - binary_sensor.office_motion
    to: "off"
    for:
      hours: 0
      minutes: 5
      seconds: 0
conditions: []
actions:
  - choose:
      - conditions:
          - condition: state
            entity_id: binary_sensor.office_motion
            state: "on"
        sequence:
          - choose:
              - conditions:
                  - condition: time
                    after: "06:00:00"
                    before: "21:45:00"
                sequence:
                  - action: light.turn_on
                    metadata: {}
                    data:
                      brightness_pct: 100
                    target:
                      entity_id:
                        - light.office_fan_light
                        - light.office_potlights
            default:
              - action: light.turn_on
                metadata: {}
                data:
                  brightness_pct: 10
                target:
                  entity_id:
                    - light.office_fan_light
                    - light.office_potlights
    default:
      - action: light.turn_off
        metadata: {}
        data: {}
        target:
          entity_id:
            - light.office_potlights
            - light.office_fan_light
mode: single
1 Like

Thank you! I have been playing with this for a bit to understand how it all works. I initially had two separate automations for turning the lights on and off.

The sample you provided worked perfectly for my ensuite but not for my hallway. How would you adjust it for the following scenario.

Hallway

  • Between 6AM and 9:45 turn on the hallway light while there is someone in the hallway.
  • Between 9:45pm and 6 AM turn on the hallway, bathroom and toilet lights to 10% for 10min, then turn off.

Would it be easier to have a scene that is controlling three lights?

No need to complicate things with a scene.

This should do exactly what you asked for, however I strongly recommend you remove the last condition and turning off the bathroom and toilet lights and just have everyone turn the lights off the old fashioned way until you have a motion sensor in the bathroom.

Eventually, somebody is going to end up stuck on that pooper for longer than 10 minutes in the middle of the night sitting in the dark with no way to turn the lights back on because the motion sensor is out in the hallway and they will not be impressed.

You need a motion sensor or better yet an mmwave presence detector in your bathroom so you can control the bathroom lights independently with a separate automation. Trust me on this.

I used an if / then / else structure instead of a choose structure in this example. It’s really more appropriate since there is only once choice. Here’s some recommended reading to help you understand what is going on here. Between that and these examples you should have a good starting reference to create other automations.

Automation Basics
Script Syntax

Again, you should be able to copy and paste this and just replace the entity id’s with the proper ones from your config.

Hallway Motion Lights Automation Example
alias: Hallway Motion Lights
description: Motion controlled hallway and bathroom lighting.
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.hallway_motion
    to: "on"
  - trigger: state
    entity_id:
      - binary_sensor.hallway_motion
    to: "off"
    for:
      hours: 0
      minutes: 10
      seconds: 0
conditions: []
actions:
  - if:
      - condition: state
        entity_id: binary_sensor.hallway_motion
        state: "on"
    then:
      - if:
          - condition: time
            after: "06:00:00"
            before: "21:45:00"
        then:
          - action: light.turn_on
            metadata: {}
            data:
              brightness_pct: 100
            target:
              entity_id:
                - light.hallway_light
        else:
          - action: light.turn_on
            metadata: {}
            data:
              brightness_pct: 10
            target:
              entity_id:
                - light.hallway_light
                - light.bathroom_light
                - light.toilet_light
    else:
      - action: light.turn_off
        metadata: {}
        data: {}
        target:
          entity_id:
            - light.hallway_light
      - condition: time
        before: "06:00:00"
        after: "21:45:00"
      - action: light.turn_off
        metadata: {}
        data: {}
        target:
          entity_id:
            - light.bathroom_light
            - light.toilet_light
mode: single

BTW have you looked into blueprints? I don’t use them myself but you might find them handy to learn from and get started quickly with different kinds of automations.

I am using the Phillips hue globes so turning off at the switch is no good. Thanks for the advice, I’ll try pickup another motion sensor.

Did the hallway automation work as expected?

And if you can find an affordable one that works with your setup, get an mmwave sensor rather than an PIR motion sensor. Saves ya from having to wave at the motion sensor every 10 minutes if you’re stuck there for a while. :laughing:

https://www.novelic.com/blog/the-advantages-of-mmwave-radar-sensors-over-pir-sensors-for-lighting-control-and-smart-lighting-applications/