Luminance Level as Condition for Sensor Lights

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.

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

Conditions both true again

Make the modification I suggested in my previous post. We need to see the actual value of sensor.kitchen_light_level in the trace so we can understand why the automation’s Template Condition considers it to be less than 6.

BTW, you overlooked to post a screenshot of sensor.kitchen_light_level that I had requested (the one you posted doesn’t show the entity’s entity_id only its friendly name) so I am assuming it is the correct name of an existing entity in your system. Because if it’s misspelled (i.e. doesn’t exist) then that would explain why the Template Condition always reports true.

Got this:

Step forward to the next node in the flowchart (because the one in the screenshot isn’t showing the variables we want).

You may have hit the nail on the head here. I’ve misnamed my sensor entity in the automation. It should be sensor.kitchen_sensor_light_level (sorry, can’t figure out how to add code quotes on the iPad as it doesn’t have back quotes)

I’ll make the changes and retest.

As I explained above, that’s one of the reasons that can cause the Template Condition to report true.

If sensor.misspelled doesn’t exist:
states('sensor.misspelled') reports unknown
states('sensor.misspelled') | float(0) reports 0
states('sensor.misspelled') | float(0) < 6 reports true

I’ve made the amendments and the variable, after 3 tests each time is showing light_level: unknown:

Post the automation’s YAML code.

In addition, post a screenshot of the sensor entity as it appears in Developer Tools > States like this:

I hadn’t renamed the entity in the state variable template but done that now and instead of ‘unknown’ it’s showing 0, so…progress.

It just triggered as expected, with a light level of 3, however, I’m finding it difficult to test now with no daylight. I will retest tomorrow and see how it goes.

Thank you for your help and time. I’ve definitely learned a lot tonight so hopefully this will work tomorrow.

I suspected that but didn’t want to second guess you (again) because earlier you said:

I’ve made the amendments and the variable

Anyway, the main mistake was supplying the template with a non-existent entity (like I explained several posts ago: “… or the specified sensor entity doesn’t exist”). The addition of the variable was just to confirm it and now it’s no longer needed given that the correct entity_id has been specified. I’m confident it will work properly tomorrow when you test it during daylight hours.

1 Like

So, I’ve had chance to play about testing now that there is natural light going into the room and have managed to successfully get the automation working in various rooms.

Thank you for your help and time, it’s much appreciated.

One last question though, can the below be put in template form? If so what would the template format be?

    - condition: time
      after: '23:00:00'
      before: '06:00:00'

You’re welcome!

Before you go, please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.
For more information, refer to guideline 21 in the FAQ.

Like this:

    - condition: template 
      value_template: "{{ now().hour < 6 or now().hour == 23 }}"

Or in shorthand notation:

    - "{{ now().hour < 6 or now().hour == 23 }}"

Yet another way:

    - "{{ now().hour in [0, 1, 2, 3, 4, 5, 23] }}"


Correction. The before: '06:00:00' option means the current hour must be 5 or less, not 6. I have corrected the suggested templates.

1 Like

That’s great, I’ll use that. Again, thank you.

1 Like

Thanks. Can I just confirm, the or in the template below is correct as opposed to and?

- "{{ now().hour < 6 or now().hour == 23 }}"

The or is correct. The template is checking if the current hour is less than 6 or equal to 23. The value cannot be a number less than 6 and equal to 23.

1 Like