Automation issue (check lux, act on light)

Hi all,
I’m trying to understand what’s wrong with my automation.
I would like to turn off a light as soon a lux sensor is over a value. Seems really easy, but it doesn’t work.
Here the code:

id: '1628526181927'
alias: Bagno Vasca - Accendi LED Soffitto
description: Bagno Vasca - Accendi LED Soffitto in base ai lux
trigger:
  - platform: numeric_state
    entity_id: sensor.0x00158d000484acdf_illuminance_lux
    attribute: illuminance_lux
    for: 10s
    below: '20'
condition: []
action:
  - type: turn_on
    device_id: ec589d1eca3f6f434b967c061c66b062
    entity_id: light.dimmer_switch_22
    domain: light
mode: single

And here the status of the two devices used:

entity: light.dimmer_switch_22
supported_color_modes:
  - onoff
color_mode: onoff
friendly_name: LED Bagno Vasca
supported_features: 0

entity: sensor.0x00158d000484acdf_illuminance_lux
state_class: measurement
battery: 100
illuminance: 35
illuminance_lux: 35
linkquality: 144
occupancy: false
temperature: 28
voltage: 3055
unit_of_measurement: lx
friendly_name: Bagno Vasca Movimento illuminance lux
device_class: illuminance

Ideas?

Thanks a lot!

Simon

trigger:
  - platform: numeric_state
    entity_id: sensor.0x00158d000484acdf_illuminance_lux
    below: '20'
    for: '00:00:10'

The for in the trigger should be like the above. Always refer to Automation Trigger documentation where you can find examples on how to write YAML syntax correctly.

Also, just a suggestion for the action, use this instead of device_id-

action:
  - service: light.turn_on
    target:
      entity_id: light.dimmer_switch_22

The reason is when you need to change the device of your light, you just need to change the entity_id to match the old one light.dimmer_switch_22 - no need to change the device_id in the automation.

Not sure where you found that syntax, but I don’t think it’s valid.

Try

for: "00:00:10"

or

for:
  seconds: 10

Hello @ardysusilo and @koying , thanks for suggestions, I’ll check it out!
I’m trying to use the GUI automation interface. I’ll let you know if it works!
Simon

Hello @ardysusilo and @koying , corrected, but nothing happens:

- id: '1628525255445'
  alias: Bagno Vasca - Spegni LED Soffitto
  description: Bagno Vasca - Spegni LED Soffitto in base ai lux
  trigger:
  - platform: numeric_state
    entity_id: sensor.0x00158d000484acdf_illuminance_lux
    attribute: illuminance_lux
    above: '20'
  condition: []
  action:
  - type: turn_off
    device_id: ec589d1eca3f6f434b967c061c66b062
    entity_id: light.dimmer_switch_22
    domain: light
  mode: single

I’ve removed the for time, and in this case I’ll act if the lux goes up, over 20lx. Actually it’s 26 but light is still on:

state_class: measurement
battery: 100
illuminance: 26
illuminance_lux: 26
linkquality: 147
occupancy: false
temperature: 29
voltage: 3055
unit_of_measurement: lx
friendly_name: Bagno Vasca Movimento illuminance lux
device_class: illuminance
entity: light.dimmer_switch_22
state: on
supported_color_modes:
  - onoff
color_mode: onoff
friendly_name: LED Bagno Vasca
supported_features: 0

Ideas?

Thanks, Simon

The automation will only be triggered when the lux goes from below to above 20.
As the current value is 26, it will not be triggered before it goes under 20, then above 20 again.

ok, here both automations:

- id: '1628525255445'
  alias: Bagno Vasca - Spegni LED Soffitto
  description: Bagno Vasca - Spegni LED Soffitto in base ai lux
  trigger:
  - platform: numeric_state
    entity_id: sensor.0x00158d000484acdf_illuminance_lux
    attribute: illuminance_lux
    above: '20'
  condition: []
  action:
  - type: turn_off
    device_id: ec589d1eca3f6f434b967c061c66b062
    entity_id: light.dimmer_switch_22
    domain: light
  mode: single

- id: '1628526181927'
  alias: Bagno Vasca - Accendi LED Soffitto
  description: Bagno Vasca - Accendi LED Soffitto in base ai lux
  trigger:
  - platform: numeric_state
    entity_id: sensor.0x00158d000484acdf_illuminance_lux
    attribute: illuminance_lux
    below: '10'
  condition: []
  action:
  - type: turn_on
    device_id: ec589d1eca3f6f434b967c061c66b062
    entity_id: light.dimmer_switch_22
    domain: light
  mode: single

Should they then work as expected? I’ll try covering the lux sensor and let you know, thanks!

Simon

Go to Developer Tools → States (tab) → Type sensor.0x00158d000484acdf_illuminance_lux → Change the state value to 15 → Set States → Change the value again to 25 → Set States.

Also, sensor.0x00158d000484acdf_illuminance_lux has states that reports in lux right? Then you do not need to add attribute in the trigger.

  - platform: numeric_state
    entity_id: sensor.0x00158d000484acdf_illuminance_lux
    above: '20'

For the action, use this- (It will make it easier for you if you decide to change the device).

action:
  - service: light.turn_on
    target:
      entity_id: light.dimmer_switch_22

Also, please have a look at the description of Numeric State Trigger.
Numeric_state trigger fires when the numeric value of an entity’s state crosses a given threshold.

Great @ardysusilo , the provlem was the attribute. As soon I’ve removed it, it works.
About the device_id, it’s mandatory to have it editable via GUI, otherwise the automation gets grayed out.
Now the question is, why the attribute was stopping it to work?

Thanks to both for the help!

Simon

Maybe the sensor does not have attribute that reports lux value as it is already reported in the states of the sensor. See my example-

Hmm, not exactly, it contains in both:

image

Btw strange.

Simon

Then probably it was because the lux is already above 20 when you save the automation.

As I said previously, it needs to be below your threshold (e.g 10) - evaluated as FALSE, then go above your threshold (e.g 25) - evaluated as TRUE. It needs to change from FALSE to TRUE for the automation to be triggered.

I don’t think that’s true. In the action, choose Call Service, find the light.turn_on, select entity target. See my example-