Light on and off automation with lux values

With the code below, what I get is this:

  1. When presence is detected, lux value is updated and light comes on as expected.
  2. When presence is cleared, light turns off and lux value updates to before the light turned off (so lux value is higher than 1200, depends on bulb brightness).
  3. When presence is detected again, light stays off, although lux value is updated to reflect lower than 1200 - I want the light to come on not stay off

I think I understand why number 3 happens, even though the lux value is updated, the automation uses the value before it is updated. I tried adding a 1-second delay before conditions, but this does nothing and isn’t really ideal.

How do I fix this?

alias: Entrance Motion Light
description: ""
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.entrance_motion_sensor_occupancy
    to:
      - "on"
conditions:
  - condition: numeric_state
    entity_id: sensor.entrance_motion_sensor_illuminance
    below: 1200
actions:
  - action: light.turn_on
    metadata: {}
    data:
      brightness_pct: 35
      color_temp_kelvin: 4292
    target:
      entity_id: light.entrance_light
  - wait_for_trigger:
      - trigger: state
        entity_id:
          - binary_sensor.entrance_motion_sensor_occupancy
        from: "on"
        to: "off"
  - action: light.turn_off
    metadata: {}
    data: {}
    target:
      entity_id: light.entrance_light
mode: single

Edit: See below for a picture of the trace where it failed.

check your traces, it will tell you why it didnt continue on what step

On top of that, you created a long running automation, thats not the best pattern to do these.
I would suggest to inlcude both triggers (form on to off and from off to on) in start section, create ID for them and make logic in action to choose the right flow based on trigger id.

somthing like:

triggers:
  - type: occupied
    device_id: ada57cc74a458a29b67cd46410905a3c
    entity_id: e57b4de445bfa2b4448f9f38b466e15a
    domain: binary_sensor
    trigger: device
    id: aanwezigheid
  - type: not_occupied
    device_id: ada57cc74a458a29b67cd46410905a3c
    entity_id: e57b4de445bfa2b4448f9f38b466e15a
    domain: binary_sensor
    trigger: device
    id: afwezigheid
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - aanwezigheid
        sequence:
          - data:
              value: "0.1"
            target:

.
.
.
      - conditions:
          - condition: trigger
            id:
              - afwezigheid
        sequence:
          - data:
              value: "0.25"
1 Like

Show the automation trace of a time when it did not come on as expected. I think something else is bugging you. For instance, if the automtion hngs in the wait for trigger, then because of the single execution the automation won’t run again. This will for instance happen when you turn the light off manually when there is still motion.

It is better to split this in two separate automations then it is to have a long running automation.

2 Likes

Your hunch is correct. Most motion sensors with built in lux sensors will only update the lux sensor value after they detect motion. Granted, it’s only a few milliseconds, but it’s a long enough delay that your automation has already run by that time.

Show where you added the delay. It should be part of your trigger, not part of the conditions.
Something like this:

triggers:
  - trigger: state
    entity_id:
      - binary_sensor.entrance_motion_sensor_occupancy
    to: "on"
    for:
      hours: 0
      minutes: 0
      seconds: 1
      #milliseconds: 500

Note I commented out the milliseconds part since that is not supported by the visual editor. Test the 1s in the visual editor first, then if it works, drop to yaml mode and test the milliseconds option.

Like @Edwin_D mentioned, looking at the automation traces will be a key part in determining why something did not behave as expected. Follow his advice and use the traces to troubleshoot.

1 Like

This solved it. I had to play around with the milliseconds value but 810 works for me. I get the same result as the trace for any value below that.

In case anyone was wondering, this was the original time delay I used

alias: Entrance Motion Light New
description: ""
triggers:
  - entity_id:
      - binary_sensor.entrance_motion_sensor_occupancy
    from: "off"
    to: "on"
    trigger: state
actions:
  - delay: "00:00:01.00"
  - choose:
      - conditions:
          - condition: numeric_state
            entity_id: sensor.entrance_motion_sensor_illuminance
            below: 1200
        sequence:
          - target:
              entity_id: light.entrance_light
            data:
              brightness_pct: 51
              color_temp_kelvin: 5534
            action: light.turn_on
          - wait_for_trigger:
              - entity_id:
                  - binary_sensor.entrance_motion_sensor_occupancy
                from: "on"
                to: "off"
                trigger: state
          - target:
              entity_id: light.entrance_light
            action: light.turn_off
mode: single
1 Like