Help with simple motion sensor automation

Tearing what little hair have left out.

Can some body please please please help a complete Home Assitant dummy with a “simple” automation.
I am trying to set up an automation to turn on a group of lights (connected by smart sockets) when the light level in a room is below a set level (17 lux) between certain times of the day (after 15:15 & before 21:30).
I am using an Aqara P1 as the motion sensor connected via Zigbee2MQTT & the smviceart plugs are a mixture of wifi & Zigbee.
Below are screen shots of the automation (I don’t do YAML & I am far too old to learn now)

I have ran a trace of the automation (below)

I’m guessing the "star icon is the P1 illumination sensor but the A/B one is I have no idea. It does say that “This step wasn’t executed” when I click on it which I guess means the automation did not run (a cross is bad right?) but WHY???
Below is the copy of the Automation config. In lines 9 to 13 the numeric values (hours, minutes, seconds & below) are correct but these are in red. IS this bad or just to look pretty??? :-

id: ‘1764087627624’
alias: Downstairs Evening Lights ON
description: ‘’
triggers:

  • trigger: numeric_state
    entity_id:
    • sensor.motion_sensor_lounge_illuminance
      for:
      hours: 0
      minutes: 0
      seconds: 0
      below: 17
      above: 0
      conditions:
  • condition: time
    after: ‘15:15:00’
    before: ‘21:30:00’
    weekday:
    • mon
    • tue
    • wed
    • thu
    • fri
    • sat
    • sun
      actions:
  • type: turn_on
    device_id: 737f26d68faa789e4f9f07b70656bc64
    entity_id: 43d4e617f9b7e1756305c4b2887a5e8c
    domain: switch
    enabled: true
  • type: turn_on
    device_id: e60a24bd87e4bca7b4df0e84caea489e
    entity_id: 40857d1fbd1cdb0ad8f95ce90d262e09
    domain: switch
    enabled: true
  • type: turn_on
    device_id: f967633373ce55f34f43627aa72abc70
    entity_id: 338eeb30fbe2ba77ed681a9502b5dddf
    domain: switch
    enabled: true
  • type: turn_on
    device_id: 8b4d49ad23b84e80f06b19c012ac3c0f
    entity_id: 1aefb3ffb1e37785214b70e53dc43a3a
    domain: switch
    enabled: true
  • type: turn_on
    device_id: 5c2e6c49458d4e608f78b6e1c6864220
    entity_id: 72fe62043f248ebbb0f2543775f97264
    domain: switch
    enabled: true
    mode: single

I can see that Home Assistant is immensely powerful but by hell it is also immensely confusing & not very intuative.

Any help for an aging wannbe Home Assistant user would be very welcome & many thanks in advance

Put the YAML code in code tags. It is unreadable in the forum post without </> code tags.

I think you are misunderstanding how triggers work, that will only fire when the lux crosses 0 or 17.

If the lux is already between 0 and 17 (or outside those), it won’t fire.

What you probably want is trigger on ANY lux change, then use AND IF to determine if between 0 and 17.

Replace your existing automation with this one.

id: '1764087627624'
alias: Downstairs Evening Lights ON
description: ''
triggers:
  - trigger: numeric_state
    entity_id: sensor.motion_sensor_lounge_illuminance
    below: 17
    above: 0
  - trigger: time
    at: '15:15:00'
conditions:
  - condition: numeric_state
    entity_id: sensor.motion_sensor_lounge_illuminance
    below: 17
  - condition: numeric_state
    entity_id: sensor.motion_sensor_lounge_illuminance
    above: 0
  - condition: time
    after: '15:15:00'
    before: '21:30:00'
actions:
  - action: switch.turn_on
    target:
      device_id:
        - 737f26d68faa789e4f9f07b70656bc64
        - e60a24bd87e4bca7b4df0e84caea489e
        - f967633373ce55f34f43627aa72abc70
        - 8b4d49ad23b84e80f06b19c012ac3c0f
        - 5c2e6c49458d4e608f78b6e1c6864220
mode: single

How it works

  • It is triggered by illuminance (when the value decreases below 17 or rises above 0) and time (at 15:15).
  • When triggered (either by illuminance or time) it checks if illuminance is within the defined range (between 0 and 17) and the time is within the desired range (between 15:15 and 21:30).
  • If the conditions are met, it turns on four switches (referencing them by their device_id).

Here’s why time is a trigger. If illuminance decreases below 17 before 15:15, the conditions will not be met (because it’s too early). However, the Numeric State Trigger will have done its job and, here’s the important part, will not trigger again until the illuminance first rises above 17 and then decreases below it (a Numeric State Trigger triggers upon the crossing of the threshold value).

The inclusion of the Time Trigger ensures that when it’s 15:15, the automation is triggered and checks the conditions again. This time they will be met (because the illuminance had decreased below 17 prior to 15:15) and the action will be executed.

FWIW, this is the usual way to handle a very common scenario: perform an action when some value lies within a certain range but only within a desired time period.


NOTE

If you want to make the automation more fault-tolerant, add this third trigger:

    - trigger: homeassistant
      event: start

Here’s what that does for the automation.

Let’s say you have a power failure at 15:00, power comes back at 16:00, and illuminance is 15. The third trigger detects when Home Assistant starts and checks the conditions. The conditions are met because it’s within the time range and the illuminance is below 17 (and the switches are turned on).

Without that third trigger, the automation fails to do anything because Home Assistant was non-functional during the power failure. The Numeric State Trigger missed the chance to detect the illuminance decreasing below 17 and the Time Trigger couldn’t trigger at 15:15.

2 Likes

Just want to add, you can also use Sun - Home Assistant to trigger when the sun comes up/down (because actual daylight will vary from specific time periods.

Another thing to note: lux sensors in motion sensors often only update when there is motion. Check the history graph of your lux sensor entity if it behaves as you need when there isn’t any motion.

2 Likes

I saw the pop up mentioning that but I could not figure out what to do. Apologies

Thank you all seems I have a lot to learn & have an up hill struggle on my hands here but your help is so much appreciated.
nice see people are really helpful on here & not condescending. Special thanks to 123 Taras, your “How it works” comment turns what to me is a completely foreign language into English, kudos to you & everybody else.

1 Like

I had no idea that’s how they worked. I thought changing to detection interval under devices governed the reporting back to Home Assistant interval. I bet that did not help matters at all. Thank you

Edwin is right. The product page for the P1 hints at this by listing Motion & Motion plus Light Detection as one of the features:

I suggest you get your hands on a dedicated Lux sensor (Aqara make these too). Light level detection is pretty much a secondary feature on most motion sensors (hence the name). A dedicated light sensor will ensure that you get constant readings without requiring any motion.

Rule 11.