Outdoor illuminance template sensor

Many thanks for your help! I removed the spaces, but the sensor is still showing up as unavailable.

Any ideas? :slight_smile:

I just realized that you need to replace “value_template” with “state”. I missed that earlier.

Thought you might find this interesting. I went from using Phil’s integration to your template to getting an actual illuminance sensor. Here’s a 5 day graph comparing your sensor to the physical illuminance sensor (weatherflow weather station if you’re curious). So I’m officially retiring the illuminance template now, but it did what it was supposed to do when I needed it. Thanks!

I spent a few hours trying to get this working but kept getting error:
UndefinedError: 'None' has no attribute 'last_updated'.

It looks like states.sensor.time.last_updated.timestamp() no longer works with my version of HA 2022.3.3. I tried changing the following:

{%- set right_now = states.sensor.time.last_updated.timestamp() %}

to

{%- set right_now = as_timestamp(now()) %}

And the sensor is now available, it’s midnight here and the value is 10 so I assume it’s working, will check in the morning.

I recall the intention of the use of sensor.time to give the template an entity to trigger an update when changed. There has been many changes to the way template work in Home Assistant since then, and this may be necessary no longer. When I tested both of your examples of code in the template developer tool, it says that the first one will update when sensor.time changes. While as_timestamp(now()) triggers “updates at the start of each minute.”

P.S. I just confirmed it still works. However, I did have to define a sensor.time.

sensor:
  - platform: time_date
    display_options:
      - 'time'

I think you’re getting a little ahead of yourself here :wink:

Hahaha… oopsie daisy. Thank you. I corrected that.

P.S. I just confirmed it still works. However, I did have to define a sensor.time.

Thanks! I missed there’s a sensor required. I’ll try changing over to your original sensor, it was working for me but I was getting odd large spikes.

I’m getting these large spikes to 7500 lx, is this normal?

It can be normal. It completely depends on what the weather integration is returning as the current condition. This can change every time it updates.

Is there any reason I shouldn’t continue to use

          {%- set right_now = now() | as_timestamp %}

It’s still working fine…

It’s still working fine…

No continue to use the original code is fine, I had to add the time_date sensor manually in order for it to work as @BrianHanifin mentioned above.

I have never used that. Is there a reason I should use {%- set right_now = as_timestamp(now()) %} when I have always used {%- set right_now = now() | as_timestamp %} and it has always worked correctly. I’m looking for a reason not a statement.

actually I meant to ask @BrianHanifin as I think we have discussed this exact thing before in this thread

2 Likes

What is the maximum lx detected by the phillips hue sensor ?
My aqara sensor is maxed out at 891 lx which is not sufficient
Thanks (I’m in France)

For anyone visiting in 2024, I converted this code to the “modern” Home Assistant format and verified it’s working. Some sun2 entity names had also changed and needed updating in the code.

Prerequisites:

  • sun2 installed from HACS and set up with the location ‘home’ (you could set it up in YAML, but if you do it through the UI it lets you very easily assign it to your geolocation, which feels preferable to me)
  • AccuWeather integration set up with the location ‘home’
  • If you have the ‘Time & Date’ integration set up already (such that it exposes sensors.time), you don’t need the corresponding lines in my config
# Outdoor Illumination - Home Assistant 2024
# This presumes you've set up sun2 and AccuWeather integrations with the location of 'home'

# Take this out if you're already using Time & Date integration
sensor:
  - platform: time_date
    display_options:
      - 'time'

# pnbruckner's sensor component as a template.
# https://github.com/pnbruckner/ha-illuminance/blob/8ba9f76709a6cb1fee2a7b73edfff0507a21acad/custom_components/illuminance/sensor.py
# https://community.home-assistant.io/t/outdoor-illuminance-template-sensor
template:
  - sensor:
      - name: "Outdoor Illuminance Educated Guessor"
        unique_id: 2
        unit_of_measurement: "lx"
        icon: "mdi:brightness-auto"
        state: >-
          {% set factors = namespace(condition='',sun='') %}

          {#- Retrieve the current condition and normalize the value #}
          {% set current_condition = states("weather.home") %}
          {% set current_condition = current_condition|lower|replace("partly cloudy w/ ","")|replace("mostly cloudy w/ ","")|replace("freezing","")|replace("and","")|replace("-", " ")|replace("_", " ")|replace("(","")|replace(")","")|replace(" ", "") %}
          
          {#- Assign a seemingly arbitrary number to the condition factor #}
          {% set condition_factors = {
            "10000": ("clear", "clearnight", "sunny", "windy", "exceptional"),
            "7500": ("partlycloudy", "partlysunny", "mostlysunny", "mostlyclear", "hazy", "hazysunshine", "intermittentclouds"),
            "2500": ("cloudy", "mostlycloudy"),
            "1000": ("fog", "rainy", "showers", "pouring", "snowy", "snowyheavy", "snowyrainy", "flurries", "chanceflurries", "chancerain", "chancesleet", "drearyovercast", "sleet"),
            "200": ("hail", "lightning", "tstorms")
          } %}
          {% for factor in condition_factors if current_condition in condition_factors[factor] %}
            {% set factors.condition = factor %}
          {% endfor %}
          
          {#- Compute Sun Factor #}
          {% set right_now = states.sensor.time.last_updated.timestamp() %}
          {% set sunrise = states("sensor.home_sun_rising") | as_timestamp %}
          {% set sunrise_begin = states("sensor.home_sun_dawn") | as_timestamp %}
          {% set sunrise_end = sunrise + (40 * 60) %}
          {% set sunset = states("sensor.home_sun_setting") | as_timestamp %}
          {% set sunset_begin = sunset - (40 * 60) %}
          {% set sunset_end = states("sensor.home_sun_dusk") | as_timestamp %}
          {% if sunrise_end < right_now and right_now < sunset_begin %}
            {% set factors.sun = 1 %}
          {% elif sunset_end < right_now or right_now < sunrise_begin %}
            {% set factors.sun = 0 %}
          {% elif right_now <= sunrise_end %}
            {% set factors.sun = (right_now - sunrise_begin) / (60*60) %}
          {% else %}
            {% set factors.sun = (sunset_end - right_now) / (60*60) %}
          {% endif %}
          {% set factors.sun = 1 if factors.sun > 1 else factors.sun %}
          
          {# Take an educated guess #}
          {% set illuminance = (factors.sun|float * factors.condition|float) | round %}
          {% set illuminance = 10 if illuminance < 10 else illuminance %}
          {{ illuminance }}

Actually, scratch that—while the code is correct in terms of registering the sensor in HA, the readings it gives are not accurate. I’m not sure whether it’s the original code, an integration of mine, or something else I changed, so proceed with caution.