Trying to set lights and motion sensors to react to time

I am very new to all of this. So far loving the challenge and all there is to learn. I’d love if someone could point me in the right direction to set up my motion sensors with my lights.

I used to use a Philips Hue Bridge with my hue lights all around my house. But I recently moved to the Conbee 2 and ZHA integration as a way to add finer control of things and get rid of all the annoying scenes that forced their way into my scenes from the Hue app.

Before in the Hue app I had it set up so that for example closet motion sensor detects motion and the time is between 20:30 and 5:30 then set the lights to closet night scene. It worked great.

Now trying to make it work myself I am struggling to get it to work. I tried to add a “or” condition to my light automation to say “night scene between 20:30 to midnight (i heard you can’t ask it to go to the next day) OR night scene between 21:30 to midnight on weekends” it seems I can mostly get it to work if I don’t add the “or” condition.

Also I am struggling to get the lights to stay on if it continues to see motion. Like right now it sees motion, turns the light on then after 2 mins it turns them off. I tried to add a “wait for trigger” function to have it wait till the sensor detected no motion for 2 minutes and then turn the lights off but it doesn’t seem stable.

Not exactly sure how to properly paste code so forgive me if I do it wrong but I added the code for my current set up in my closet. But it is pretty identical to my kitchen and bathroom as well.

- id: '1648644021501'
  alias: Closet Night Motion
  description: ''
  trigger:
  - type: occupied
    platform: device
    device_id: 71b524a01136769411dcf9b61b30570a
    entity_id: binary_sensor.closet_motion_sensor_occupancy
    domain: binary_sensor
  condition:
  - condition: time
    before: 00:00:00
    after: '20:30:00'
    weekday:
    - mon
    - tue
    - wed
    - thu
    - fri
    - sat
    - sun
  action:
  - service: scene.turn_on
    target:
      entity_id: scene.closet_night_light
    metadata: {}
  - wait_for_trigger:
    - type: not_occupied
      platform: device
      device_id: 71b524a01136769411dcf9b61b30570a
      entity_id: binary_sensor.closet_motion_sensor_occupancy
      domain: binary_sensor
      for:
        hours: 0
        minutes: 0
        seconds: 30
  - delay:
      hours: 0
      minutes: 1
      seconds: 0
      milliseconds: 0
  - type: turn_off
    device_id: 6cef956df95fe255be5dea3ee4652d5d
    entity_id: light.closet_large_side
    domain: light
  - type: turn_off
    device_id: 0683e733bff9c4896c2657168b06a3d1
    entity_id: light.closet_small_side
    domain: light
  mode: single
- id: '1648645710667'
  alias: Closet Day Motion
  description: ''
  trigger:
  - type: occupied
    platform: device
    device_id: 71b524a01136769411dcf9b61b30570a
    entity_id: binary_sensor.closet_motion_sensor_occupancy
    domain: binary_sensor
  condition:
  - condition: time
    before: '20:30:00'
    after: 06:30:00
    weekday:
    - mon
    - tue
    - wed
    - thu
    - fri
    - sat
    - sun
  action:
  - service: scene.turn_on
    target:
      entity_id: scene.closet_day_light
    metadata: {}
  - wait_for_trigger:
    - type: not_occupied
      platform: device
      device_id: 71b524a01136769411dcf9b61b30570a
      entity_id: binary_sensor.closet_motion_sensor_occupancy
      domain: binary_sensor
      for:
        hours: 0
        minutes: 2
        seconds: 0
  - delay:
      hours: 0
      minutes: 1
      seconds: 0
      milliseconds: 0
  - type: turn_off
    device_id: 6cef956df95fe255be5dea3ee4652d5d
    entity_id: light.closet_large_side
    domain: light
  - type: turn_off
    device_id: 0683e733bff9c4896c2657168b06a3d1
    entity_id: light.closet_small_side
    domain: light
  mode: single 

For repeated triggers I think you should use a restart mode Automation Modes - Home Assistant

Ah so that it will keep from turning off? Better than using the “wait for trigger” in the automation?

I think it will kick the automation off again.

You can try this:

It listens for a state change in the motion sensor.
On state change, it chooses what to do:
→ motion ‘on’ between 20:30 and 5:30 → switch on the scene
→ motion ‘off’ for 1 minute → switch lights off

alias: Closet Night Motion
description: ''
mode: restart
trigger:
- platform: state
  entity_id: binary_sensor.closet_motion_sensor_occupancy
action:
- choose:

    - conditions:
        - condition: state
          entity_id: binary_sensor.closet_motion_sensor_occupancy
          state: 'on'
        - condition: time
            before: '5:30:00'
            after: '20:30:00'
      sequence:
        - service: scene.turn_on
          data: {}
          target:
            entity_id: scene.closet_day_light
    
    - conditions:
        - condition: state
          entity_id: binary_sensor.closet_motion_sensor_occupancy
          state: 'off'
          for:
            hours: 0
            minutes: 1
            seconds: 0
      sequence:
        - service: light.turn_off
          data: {}
          target:
            entity_id:
              - light.closet_large_side
              - light.closet_small_side

  default: []