Looking for help motion based on light level + time of day

Hello everyone,

I’ve been trying for a while to get a specific automation to work without succes. Today I’ve done some stuff wrong (unrelated) and I needed to start on a fresh instal so good time to try again.

What i want is a motion trigger to turn on a light for 30 seconds which refreshes as long as motion keeps being detected. As a condition I would use lux levels.
In addition to that I want the light to turn on on a different brightness depending on the time off day (basically betweem 23.00-05:00, if lux level is low, go on 5% for example)

I also would prefer (but not needed) to have 1 automation per light. On the old instal I had 1 to trigger the light on and another one to turn it off if there is no motion but I would like a less cluttered automations tab if possible.

I feel like it shouldn’t be that difficult but I’ve been at this for way too long.

Here’s an example, using 1 automation for on and off turned out to be problematic, as re-triggers and conditions wasn’t handled properly, but that could just be my lack of know how, so I use 1 automation to turn on and 1 to turn off.

I use light.turn_on twice because my switches “Lutron Caseta” takes too long to turn on at a dimmed state, it turns on faster at full brightness, so I turn them on at full and then dim them with another service call consecutively, hope this helps.

automation:
  - alias: Master Bathroom Lights On
    initial_state: true
    mode: restart
    trigger:
      platform: state
      entity_id: binary_sensor.mbath_motion
      to: 'on'
    condition:
      condition: and
      conditions:
        - condition: state
          entity_id: 'light.master_bathroom_lights'
          state: 'off'
        - condition: numeric_state
          entity_id: 'sensor.master_bathroom_motion_luminance'
          below: 7
    action:
      - service: light.turn_on
        data:
          entity_id: light.master_bathroom_lights
      - service: light.turn_on
        data_template:
            {% if now().hour < 7 %}
              20
            {% else %}
              50
            {% endif %}
          entity_id: light.master_bathroom_lights

This depends on how your motion detector, and the associated entity, work. E.g., is it a binary sensor that stays on as long as there is motion? Or does the binary sensor go off and back on while there is motion? How you would implementation the automation depends on this.

Coincidentally I just created an automation that responds to a binary sensor that goes on when motions starts, and stays on (the binary sensor, that is) at least one minute. During that time if motion is detected again it stays on. So basically the binary sensor comes on when motion is detected, and stays on until no motion has been detected for one minute. So it will be on at least one minute, or it could be on indefinitely.

I also wanted this to only turn the light on if it was dark out, and if the light was not already on. (BTW, it would also trigger when it got dark if the motion sensor was already on at that time.) It would keep the light on for at least 9 minutes after the motion sensor went back off (so a total minimum of 10 minutes.) Lastly, if the automation was still running when it got light out, it would stop and turn the light off.

The motion sensor is binary_sensor.by_motion. And it also uses binary_sensor.dark_outside, which is a template binary sensor that is on when a light sensor registers below a certain level. Here it is:

binary_sensor:
  - platform: template
    sensors:
      by_motion_dark:
        value_template: >
          {{ is_state('binary_sensor.by_motion', 'on') and
             is_state('binary_sensor.dark_outside', 'on') }}

automation:
  - alias: Backyard Motion
    mode: single
    max_exceeded: silent
    trigger:
      - platform: state
        entity_id: binary_sensor.by_motion_dark
        to: 'on'
    condition:
      - condition: state
        entity_id: switch.backyard_light
        state: 'off'
    action:
      - alias: Turn on backyard light
        service: switch.turn_on
        entity_id: switch.backyard_light
      - repeat:
          while:
            - condition: state
              entity_id: binary_sensor.by_motion_dark
              state: 'on'
          sequence:
            - alias: Wait for motion to stop or to get light
              wait_template: "{{ is_state('binary_sensor.by_motion_dark', 'off') }}"
            - choose:
                - conditions: "{{ is_state('binary_sensor.dark_outside', 'on') }}"
                  sequence:
                    - alias: Wait for motion to start again or to get light
                      wait_template: >
                        {{ is_state('binary_sensor.by_motion', 'on') or
                           is_state('binary_sensor.dark_outside', 'off') }}
                      timeout: '00:09:00'
      - alias: Turn off backyard light
        service: switch.turn_off
        entity_id: switch.backyard_light

That sounds awesome ill look into this for sure this sounds like it could work the way i want it too.

The only part this doesn’t do is the varying brightness depending time of day and/or light level (mainly because in my case it’s a simple on/off switch, not a dimmable light.) That should be as simple as adding an appropriate template for brightness to the light.turn_on service call.