Code last updated: Nov 22, 2021
Status Update: Sept 24, 2020
Grab a copy of the template code below and start using it! The intent of this code is to give all of you (that aren’t comfortable with Python) the ability to customize the condition factors and even the sun factor in any way that works best for you.
The template code is performing very well! I started this as a personal challenge just to see if I could do it, and I did. I don’t plan on updating the code much in the future. Consider this your starting point!
By the way, I ran it side-by-side with https://github.com/nkm8/ha-illuminance and this was the result overnight! The reason for the slight difference in timing is ha-illuminance estimates dawn and dusk, while I am getting, what I assume is a more accurate time from @pnbruckner’s sun2 component. Due to this comparison I modified the template code to keep the lowest lx value at 10 instead of 0.
sensor.illuminance - template sensor
# pnbruckner's sensor component as a template.
# https://github.com/pnbruckner/ha-illuminance/blob/master/custom_components/illuminance/sensor.py
platform: template
sensors:
illuminance:
friendly_name: Outdoor Illuminance Educated Guessor
icon_template: mdi:brightness-auto
unit_of_measurement: lx
value_template: |
{%- set factors = namespace(condition='',sun='') %}
{#- Retrieve the current condition and normalize the value #}
{%- set current_condition = states("weather.santee") %}
{%- 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.sunrise") | as_timestamp %}
{%- set sunrise_begin = states("sensor.dawn") | as_timestamp %}
{%- set sunrise_end = sunrise + (40 * 60) %}
{%- set sunset = states("sensor.sunset") | as_timestamp %}
{%- set sunset_begin = sunset - (40 * 60) %}
{%- set sunset_end = states("sensor.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 }}
sun2 sensor config
platform: sun2
monitored_conditions:
- sunrise
- sunset
- dawn
- dusk
Please see the CHANGELOG post below if you’d like to see any past changes.
Original Post
Poor @pnbruckner, he created a terrific outdoor illuminance sensor component. Unfortunately every time he adds support for a new weather component, support for that service get pulled for one reason or another.
I took a look at the code today and decided it would be a nice challenge to attempt to convert his Python code into a Home Assistant sensor template. I think I have succeeded. I haven’t had it running for more than a few hours, and I have already caught a few mistakes. I made use of @pnbruckner’s sun2 component to gather today’s sunrise and sunset values.
The nice thing about this is it is a template that we can modify to add support to the list of conditions for any weather provider. I’m using Accuweather as my primary weather provider, and I tried to create a fairly comprehensive list of conditions by referencing their Weather Icons API. I added most of their condition text values in the condition_factors
dictionary. I also use some replacement filters in an attempt to make conditions be more consistent.