Best way to check if its dark outside/inside

Hi, i am currently trying to find a (nearly) perfect automation for my motion activated lights.
I only want the automation to get triggered when its dark (or when i really need some lighting)

I have some aqara, hue und ikea motion sensors. Aqara und Hue have an illuminance sensor so this could be a thing. But i’m confused with a illuminance sensor which triggers a light in the same room. Will all automations still work?

Ikea has no illuminance so i need some tips if i need to stick with the built in illuminance sensors.

Here is my automation for a light. The illuminance condition should be only for turning on, but not for turning off otherwise the light will never be turned off because its lighter in my room?

alias: Flur Licht Motion
trigger:
  - platform: state
    entity_id: binary_sensor.motion_flur_presenz
    from: 'off'
    to: 'on'
    id: 'on'
  - platform: state
    entity_id: binary_sensor.motion_flur_presenz
    from: 'on'
    to: 'off'
    for: '00:00:30'
    id: off_30
  - platform: state
    entity_id: binary_sensor.motion_flur_presenz
    from: 'on'
    to: 'off'
    for: '00:01:30'
    id: off_50
condition:
  - condition: or
    conditions:
      - condition: numeric_state
        entity_id: sensor.sunlight_pct
        below: '20'
action:
  - variables:
      entity: light.extended_color_light_1_2
      light_on: '{{ is_state(entity, ''on'') }}'
  - choose:
      - conditions:
          - condition: template
            value_template: '{{ trigger.id == ''on''}}'
        sequence:
          - service: light.turn_on
            target:
              entity_id: '{{ entity }}'
            data:
              brightness_pct: 75
      - conditions:
          - condition: template
            value_template: '{{ trigger.id == ''off_30'' and light_on }}'
        sequence:
          - service: light.turn_on
            target:
              entity_id: '{{ entity }}'
            data:
              brightness_pct: 25
      - conditions:
          - condition: template
            value_template: '{{ trigger.id == ''off_50'' and light_on }}'
        sequence:
          - service: light.turn_off
            target:
              entity_id: '{{ entity }}'
    default: []
mode: single

I also tried to get a dark outside template with these templates:

- platform: template
  sensors: 
    dark_outside:
      value_template: >-
        {%- if (states.sun.sun.attributes.elevation | int < 7.5 ) -%}
              True
        {%- elif ( (states.sun.sun.attributes.elevation | int < 25) and (states.sensor.openweathermap_cloud_coverage.state | int > 70)) -%}
              True
        {%- elif (states.sensor.openweathermap_cloud_coverage.state | int > 95) -%}
              True
        {%- else -%}
              False
        {%- endif -%}

and

- platform: template
  sensors:
    ## Calculates daylight brightness out of 100%
    sunlight_pct:
      value_template: >-
        {%- set elevation = state_attr('sun.sun','elevation') | float %}
        {%- set cloud_coverage = states('sensor.openweathermap_cloud_coverage') | float %}
        {%- set cloud_factor = (1 - (0.75 * ( cloud_coverage / 100) ** 3 )) %}
        {%- set min_elevation = -12 %}
        {%- set max_elevation = 60 %}
        {%- set adjusted_elevation = elevation - min_elevation %}
        {%- set adjusted_elevation = [adjusted_elevation,0] | max %}
        {%- set adjusted_elevation = [adjusted_elevation,max_elevation - min_elevation] | min %}
        {%- set adjusted_elevation = adjusted_elevation / (max_elevation - min_elevation) %}
        {%- set adjusted_elevation = adjusted_elevation %}
        {%- set adjusted_elevation = adjusted_elevation * 100 %}
        {%- set brightness = adjusted_elevation * cloud_factor %}
        {{ brightness | round }}
      unit_of_measurement: '%'
      device_class: 'illuminance'
    ## Daylight brightness converted to opacity for CSS
    sunlight_opacity:
      value_template: >-
        {%- set sunpct = states('sensor.sunlight_pct') | float %}
        {%- set opacity = sunpct / 100 | float %}
        {{ opacity }}

But this is not as i need it. I think its a bit dark in my apartment. One site has a tree infront. So one site is brighter than ther other. I currently trigger the light with an offset after sunrise and before sunset.

The automation should work in summer and winter.

Do you have any tips? How other guys are handling with this? What would the best option to have a sensor/entity to know if its dark?

Are you sure you’re not overthinking it a bit?

You’re obviously concerned that it feels dark - why not establish comfortable light levels for each room and write them into the automations?

Yes but this presumes i have an illuminance level sensor in each room?

I changed my automation so light will only turn on when illuminance is under a certain level.
turn off automation should not be affected by this. i will test this now

but there is no chance to take the illuminance from my hallway for my motion automation in the kitchen? i ask because illuminance would change if lights in the hallway are turned on and the trigger is not true because its too bright. any tips for this?

Be advised that the Xiaomi motion sensors only send illuminance when they detect motion, or once an hour if no motion. So this might not be a good idea.

1 Like

thanks for the advice. but this should not be a problem when im in the room where the motion sensor is?
it is only a problem when the illuminance shoould trigger a different room?

indeed so .

I’m having a sensor for the same reason and at first I used a template with sun elevation and cloud coverage. But that wasn’t very precise.

That didn’t really work for me. The illuminance sensor weren’t updated that quick.

I ended up using a xiaomi illumination sensor which updates every second or so.
I could place them in every room but for now it works fine with a sensor on my balcony. If one room would be much darker than the another ones I could adjust the automation for that.

so im not the only one who has problems with that. did not know xiaomi had some illuminance sensors.
its not my favorite option to buy new hardware to get rid of my issue but if its the only one i will consider to buy a new sensor

How about using the sensor in the brightest room as a reference point? If that light is coming on, all the others should be too. If it is not, then it is safe to use light levels from that sensor to turn the others on appropriately.

Call me dumb but I think this won’t work. If light will go on in this room the automation isn’t working. I think another sensor which is not effected b other lights is the only option. But I’m still hoping for a non hardware solution

There’s an old thread elsewhere in the forum that attempts to achieve the same thing (exterior light level without using a light sensor) and the result is not entirely successful (false positives/negatives). Perhaps employing multiple references with a Bayesian sensor would be more accurate (but I don’t have any desire to explore it; I use an externally mounted light sensor).

Me too, actually.

which light sensors do you use? xiaomi?
or a motion sensor with built in light sensor?

You’re right. The sources of natural light external to the room (i.e. windows, skylights) are what need to be monitored. They should be mounted and shielded such that they’re not influenced by the lights controlled by automation.

You might be able to estimate the contribution lights in adjacent rooms if they’re under HA control.

Philips Hue external motion sensor with built-in light sensor (it also turns on security lighs in the yard). Battery operated like their internal devices, so easy to fit, but I agree, a bit expensive.

Wouldn’t this work?

- id: '1631297599374'
  alias: Light test 1
  description: Sets flag when light falls below base level in brightest room in the
    house.
  trigger:
  - platform: numeric_state
    entity_id: sensor.kitchen_motion_sensor_light_level
    below: '30'
    for:
      hours: 0
      minutes: 0
      seconds: 0
      milliseconds: 0
  condition: []
  action:
  - service: input_boolean.turn_on
    target:
      entity_id: input_boolean.lights_on
  mode: single
- id: '1631297862464'
  alias: Light test 2
  description: Turns on lights when movement is detected and light in brightest room
    is below base level, or when light in brightest room falls below a higher level.
    Can be duplicated for any room with a motion sensor (including the brightest room).
  trigger:
  - type: motion
    platform: device
    device_id: fbb1c649f6574e56d2b870c747c7d840
    entity_id: binary_sensor.living_room_motion_sensor_motion
    domain: binary_sensor
  condition:
  - condition: or
    conditions:
    - condition: state
      entity_id: input_boolean.lights_on
      state: 'on'
    - condition: numeric_state
      entity_id: sensor.kitchen_motion_sensor_light_level
      below: '40'
  action:
  - service: switch.turn_on
    target:
      entity_id: switch.living_room_spotlights
  mode: single

All lights turn off x minutes after motion is no longer detected.

The input_boolean would have to be reset at some point - perhaps at a preset time every day, like an hour after dawn - but the light in the room with the sensor would only come on after lights in darker rooms had been enabled.

Doesn’t take account of cloudy days, of course, or leaves on the tree in front of your building, but as @123 says, this sort of thing is never entirely successful.

The code should work for this but i still need an extra motion/light sensor.
I still have an aqara packaged which i could swap with the hue one in my hallway. but i need to rethink of that. still playing around with my sunlight sensor.

I think the right offset with sunset and sunrise would be enough but im not sure if summer/winter would work both. actually my sunlight sensor does the same (elevation of the sun) but with cloud coverage.

Easiest way would be to set the offsets a little bit higher but this week it was very cloudy and dark and light did not turned on so i want something else than “just time”

If im not satisfied with it i will buy a new sensor to get it 100%.

After I updated to version 2021.12.10 I lost the attribute ‘daylight’ in my HUE outdoor motion sensor. This attribute has the condition ‘true’ when the lux value goes over a thresshold and ‘false’ when it goes under the tresshold. I use it to turn on my lights when it gets dark.

Did you also noticed that this attribute is gone? Do you have an idea how to get it back?