Outdoor illuminance template sensor

There is a math translation error. The results are incorrect currently. I realized this morning that I could probably use @craigb’s pyscript component to evaluate the original code and this has been incredibly helpful!

I was able to set both my template script and the pyscript code to the same time of day to compare their sun factor values. With both set 1 second before the sunrise_end time (7:16:02 am) the Python code output 0.9997222222222222 while my template produced 0.16665227643869543.

Python/pyscript

now = datetime.datetime(2020, 9, 23, 7, 16, 2)

Home Assistant Template
A note on hour=(7 +7): to make it easier for my brain I included the timezone offset to set the UTC time to match my local, California time.

{%- set test_time = states.sensor.time.last_updated.replace(hour=(7 +7),minute=16,second=2) %}
{%- set right_now = test_time.timestamp() %}

My brain is overheated. I’m going to park it for a while. But now that I know where the problem is, I should be able to figure it out.

1 Like