Luminance Level as Condition for Sensor Lights

Hi

I want to build an extra condition into the below existing condition that checks a sensor luminance level for the room to decide on whether to turn on the lights or not. I.E, if the room luminance is already at the specified level, the automation should not run. However, I’m stuck as to what to use for this part of the condition so any help would be appreciated.

Existing Automation:

- alias: Kitchen Lights On Night
  id: kitchen_lights_on_night
  mode: single
  trigger:
  - platform: state
    entity_id: binary_sensor.kitchen_sensor_motion
    to: 'on'
  condition:
    condition: and
    conditions:
    - condition: time
      after: '23:00:00'
      before: '06:00:00'
    - condition: template
      value_template: "{{ is_state('sensor.martins_iphone_battery_state', 'Charging')and
        is_state('sensor.rachels_iphone_battery_state', 'Charging') }}"
  action:
  - service: script.kitchen_night

State Attributes: (included these because I don’t know which attribute to use as light_level as you can see below seems high. There is a separate Status in Dev Tools States that has the value 6 at the moment.

state_class: measurement
light_level: 8347
light_level_valid: true
unit_of_measurement: lx
device_class: illuminance
friendly_name: Kitchen Sensor Illuminance
  condition:
    - condition: time
      after: '23:00:00'
      before: '06:00:00'
    - "{{ is_state('sensor.martins_iphone_battery_state', 'Charging') }}"
    - "{{ is_state('sensor.rachels_iphone_battery_state', 'Charging') }}"
    - "{{ states('sensor.kitchen_sensor_illuminance') | float(0) < 50 }}"
  • Change sensor.kitchen_sensor_illuminance to the name of the entity you have that reports the light level in your kitchen.
  • Change 50 to a value below which you consider to be dim enough to require the lights to be turned on.

NOTE

  • Multiple conditions are logically ANDed by default.
  • The example I posted uses Template Conditions in shorthand notation.

EDIT

I assume the device is a Philips Hue motion detector. It reports illuminance as a sensor entity. The sensor’s state value represents the current illuminance (light level) in lux. The light_level attribute also represents illuminance but with a different unit of measurement.

1 Like

Could you help provide plots for the light_level (in lx) vs the status (unit unknown) of the same sensor, over a couple of days? By doing so would help you understand whether they are somehow correlated, whether they are noisy, and maybe a threshold.

8347 lux as a measurement value seems fine, though. Direct sunlight could go all the way to ~100000.

Also, which luminance sensor is this?

1 Like

Thanks 123. I will give that template a try but, after playing around with different light levels (up to full brightness) in the room where the sensor is located, and using the template editor in Dev Tools to get a value, it appears that the light level is only read when motion is sensed (they are Hue sensors). Or, is that how sensors work? I thought each time the light level of the room changed it would automatically be sensed by the sensor and the value captured. Maybe I’m expecting too much of the sensor capabilities?

This is obviously a different issue but I would need to force the sensor to obtain a light level value in the automation, prior to the condition, is that correct?

I have tested my other Hue sensors and they are the same.

Hi,

Most motion sensors only report the lx when they are triggered - so you can work out if turning lights on is a good idea etc.

You really need a dedicated luminance sensor. They are pretty cheap and very very useful. I have Aqara ones.

1 Like

Thanks Neil. I’ll take a look at the Aqara.

Yes, that’s how my Hue motion sensor behaves as well.

You can use the homeassistant.update_entity service call.

1 Like

Ah, great, thank you, I’ll add that service call.

After thinking about the sensor reading the light level on motion trigger, it’s logical that it does that, I’m guessing, because otherwise it would go into a sort of loop as a person moves around and keeps triggering the sensor after the initial automation is launched.

I’ve been testing the updated automation but it doesn’t work. The room (kitchen) has plenty of natural light coming in, but the lights still turn on. I’ve even lowered the value in the template down to 2, and the lights still turn on.

This the the automation for one of the rooms that I’ve included the luminance template in:

- alias: Kitchen Lights On Day
  id: kitchen_lights_on_day
  mode: single
  trigger:
  - platform: state
    entity_id: binary_sensor.kitchen_sensor_motion
    to: 'on'
  condition:
    condition: and
    conditions:
    - condition: template
      value_template: "{{ states('sensor.kitchen_light_level') | float(0) < 2 }}"
    - condition: time
      after: '06:00:00'
      before: '23:00:00'
  action:
  - service: script.kitchen_bright

Can anyone see anything wrong with the above?

EDIT - Room luminance level was 39 when testing

Did you try it by using from the automation section with run automation or by generating motion and triggering the automation?
If first, the conditions are not used, only the action will be called. You have to generate a motion or set the binary_sensor.kitchen_sensor_motion to ‘on’ in the developer tools.
The automation looks fine, it wouldn’t be executed if the state of sensor.kitchen_light_level is below 2.

How were you testing it? By walking into the kitchen to trigger the motion detector or by manually executing the automation (Run Actions)? Manual execution skips the trigger and condition.

If you tested it by entering the kitchen, check the automation’s trace to learn more about what happened.

I was testing by triggering the sensor (walking into the kitchen).
This is the last of the traces… I don’t understand it though :grimacing: so any help appreciated.

And this too. Not sure where .color comes from in the below as it’s not in the automation or script.

You said that room luminance level was 39 at the time when the test occurred.

That means the value_template should have reported false because 39 is not less than 6.

{{ states('sensor.kitchen_light_level') | float(0) < 6 }}"

However the trace indicates the template reported true. The only way that can happen is if the sensor’s value is below 6 or it isn’t a number in which case the float filter will report a default value of 0 and that’s less than 6. If the sensor’s value isn’t a number it’s because it’s unavailable or unknown or the specified sensor entity doesn’t exist.

Go to Developer Tools > States, find sensor.kitchen_light_level and post a screenshot of it.

What’s in the script?

I usually add illuminance and motion sensor as trigger and conditions to eliminate this issue.

If illuminance goes below x or motion is sensed, trigger the automation with condition; illuminance is below x and motion is sensed.

Room level was 39 during my tests earlier today but the above traces are taken after that as I couldn’t go back that far due to the last 5 limitation on traces.

States screenshot:

The value of sensor.kitchen_light_level is 7 now so if you enter the kitchen now it should not execute the script because 7 isn’t below the threshold value (6).

It’s showing 18 now but just triggered the sensor and the lights came on. I’m confused!

Check the trace that was generated for the test you just performed. Focus on the Template Condition’s result.


If you want to see the sensor’s value at the moment the automation executes, make the following changes, repeat the test, then look at the light_level variable in the trace.

  action:
  - variables:
      light_level: "{{ states('sensor.kitchen_light_level') }}"
  - service: script.kitchen_bright

Conditions both true again