Luminance Lux sensor - with delay

Hi!

I have a Xiaomi Aqara Zigbee Luminance Sensor. I will use it instead of my Sun sensor to control the outdoor lights during the dark period of the day.

I want to add some kind of delay. So it have to be below a certain lux level for at least x minutes, before my lights are switched on/off. To prevent flickering during cloud/sunset/sunrise.

The numeric state and state trigger supports exactly what you’re after, with a for: statement.

automation:
  trigger:
    platform: numeric_state
    entity_id: sensor.light
    below: 25
    for: '00:02:00'
2 Likes

Thanks for pointing it out! :slight_smile:

I just saw some strange things happening all morning, at sunrise… The sunset was triggered some minutes before the sunrise, but the illuminance valu have not been higher than 5 during this time.

This is the sunset automation:

- id: '1599502004663'
  alias: SolnedgĂĄng Lux
  description: ''
  trigger:
  - below: 5
    device_id: 76dd020b05bd4785afb1594ca40b0190
    domain: sensor
    entity_id: sensor.ljusmtare_illuminance
    for:
      hours: 0
      minutes: 3
      seconds: 0
    platform: device
    type: illuminance
  condition: []
  action:
  - data: {}
    entity_id: group.utomhusbelysning
    service: homeassistant.turn_on
  - data: {}
    entity_id: group.fonsterlampor
    service: homeassistant.turn_on
  mode: single

This is the sunrise automation:

- id: '1599502178507'
  alias: SoluppgĂĄng Lux
  description: ''
  trigger:
  - above: 5
    device_id: 76dd020b05bd4785afb1594ca40b0190
    domain: sensor
    entity_id: sensor.ljusmtare_illuminance
    for:
      hours: 0
      minutes: 3
      seconds: 0
    platform: device
    type: illuminance
  condition: []
  action:
  - data: {}
    entity_id: group.utomhusbelysning
    service: homeassistant.turn_off
  - data: {}
    entity_id: group.fonsterlampor
    service: homeassistant.turn_off
  mode: single

This is the data for the illuminance sensor.

As you we can see the value stays below 5, until 6:07:22 AM. But the automation are triggered at 6:00:26 AM…

image

What am I missing here?

I can see the behavior are the same every morning… To me it seems to bo quite strange.

Is it the exact same time every morning?
You could add a simple notification or log entry to see what the state of sensor.ljusmtare_illuminance actually is.

    - service: system_log.write
      data_template:
        level: info
        logger: ljusmtare.illuminance
        message: >
          From: {{ trigger.from_state.state }} To: {{ trigger.to_state.state }}. Changed: {{ trigger.to_state.last_changed }}

I did check this morning. Here is what I found.

The lux-level have been 1 lx from 19:38 to 06:01:06, when it goes up to 2.
3 minutes later, 06:04:06 the “sunset automation” alias: Solnedgång Lux was executed.

So the value have not been higher than 5 as the trigger defines.

It seem like the automation can be executed, without passing the trigger-value.
Please help me out here…

Do you have the log entries as i suggested?
It sounds to me like something else is running the automation. You don’t have some old debug-stuff laying around that does an “automation.trigger” or something?

No, I’m quite sure I don’t have any other automatons triggering this. But I’ll add it and check tomorrow!
Seems to be something when going from 1 to 2 lx during the morning.

Hmm, I must be missing some part…

Automation name in dev tools is automation.solnedgang_lux
The complete automation:

- id: '1599502004663'
  alias: SolnedgĂĄng Lux
  description: ''
  trigger:
  - below: 5
    device_id: 76dd020b05bd4785afb1594ca40b0190
    domain: sensor
    entity_id: sensor.ljusmtare_illuminance
    for:
      hours: 0
      minutes: 3
      seconds: 0
    platform: device
    type: illuminance
  condition: []
  action:
  - data: {}
    entity_id: group.utomhusbelysning
    service: homeassistant.turn_on
  - data: {}
    entity_id: group.fonsterlampor
    service: homeassistant.turn_on
  - data_template:
      level: info
      logger: ljusmtare.illuminance
      message: 'From: {{ trigger.from_state.state }} To: {{ trigger.to_state.state
        }}. Changed: {{ trigger.to_state.last_changed }}'
    service: system_log.write
  mode: single

Got the following error when I was triggering the automation myself:

ERROR (MainThread) [homeassistant.components.automation.solnedgang_lux] SolnedgĂĄng Lux: Error executing script. Unexpected error for call_service at pos 3: Error rendering data template: UndefinedError: 'trigger' is undefined

Have I referred the wrong object?

When you trigger the automation manually, there is no “trigger”, so that error is expected.

Ok, checked this today again… Now in the latest HA, the triggering device are clear visible. So its actually no other automation that triggers this.
Do I need to add some additional condition, like trigger only if sun is not rising? According to my logging the illuminance have not been higher than 5 when the automation was triggered.

Can the automation trigger multiple times, even if the value does not rise above the threshold?

It should not. But it could be that you get a bad measurement or a single measurement of a wrong value, which for some reason is not visible in the logs.
Also, if the automations are reloaded or HA is restarted, the threshold will be reset, so the first measurement after a reload or restart could trigger an automation.

Ah, maybe it are related to HA restart during the evening. I must check the logger for a evening with no HA restart. That can make sense.

This morning the automation triggers on the first value since the restart of HA (I guess). It have been 1lux all the night, but 3 minutes before the automation triggered, the illuminance changed to 2lux and hence triggered the automation.

Is there a way of letting the automation be persistent during restarts? Or should I just a condition to restrict triggering during the morning (in this case)?

Maybe checking if the group are on/off as a condition. I have some ideas. :slight_smile: At last can understand why it’s triggered… I start to remember I have seen mornings with no triggering (that’s maybe when the HA didn’t get restarted during the evening).

Thanks!

What you could do is use a template binary sensor, with delay_on and delay_off so that it requires the light level to be above, or below, for three minutes before it changes state. Then short blips won’t matter, and you can use that in your automation trigger.

I am pretty sure he is right in that the issue is the restarts.

Ok just checked the logbook of this morning. It was no unexpected trigger of the sunset automation during the morning (when going from 1lux to 2lux).
And, I didn’t fiddle around with HA yesterday at all… So it have to do with the restarts.

I guess I can trigger the automation by pointing with a flashlight on the illuminance-sensor during the night/evening after a restart (when value stays at 1 lux). When the sensor reports the first value (below the threshold), the automation will trigger after the delay-time (3min).

Maybe I can live with it, or I have to do a special solution as @Tinkerer proposes.
Thanks @Olen for pointing me in the right direction!