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.
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.
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.
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.
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
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 so any help appreciated.
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.
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.
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).
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.