Circadian light with Philips Hue, independent from world clocks ;-)

Me too, same location. :slight_smile:
I’m trying to use another method to achieve the same result.
I like very much the idea, thank you for that @mastermarkush!
Here is my solution:

- platform: template
  sensors:
    max_elevation:
      friendly_name: 'Maximum solar elevation angle'
      unit_of_measurement: '°'
      value_template: >
        {% set deg = pi / 180.0 %}
        {% set N = now().strftime("%j") | int %}
        {% set L = state_attr('zone.home','latitude') | float %}
        {% set D = (-1 * asin(0.39779 * cos(0.98565 * deg * (N + 10) + 1.914 * deg * sin(0.98565 * deg * (N - 2))))) / deg %}
        {{ (asin(sin(L * deg) * sin(D * deg) + cos(L * deg) * cos(D * deg)) / deg) | round(2) }}
    colortemp:
      friendly_name: 'Circadian light'
      unit_of_measurement: 'mired'
      value_template: >
        {% set mired_min = 200 %}
        {% set mired_max = 350 %}
        {% set elev_at_mired_max = -6 %}
        {% set mired_night = 390 %}
        {% if state_attr('sun.sun', 'elevation') > elev_at_mired_max %}
          {{ (mired_max - ((state_attr('sun.sun', 'elevation') | float) - elev_at_mired_max) * (mired_max - mired_min) / ((states('sensor.max_elevation') | float) - elev_at_mired_max) ) | int }}
        {% else %}
          {{ mired_night }}
        {% endif %}

This is under testing yet. You can easily change the limits of the light warmth with mired_min, mired_max and mired_night.
I stretched the diagram to civil twilight with elev_at_mired_max = - 6, but you can change it to 0 to set it to sunset/sunrise.

2 Likes

I’d love to understand what that max_elevation is doing but I think it’s beyond my maths skills. Summarising, it uses the current time and the installation’s latitude to produce our max angle (somehow). And then the new circadian value, rather than using Markus’s day length factor, does some magic with this angle and the sun’s current elevation?

I hadn’t even begun to get my head around what Markus was doing, I’m terrible at trigonometry so now I’m officially lost. That’s OK, it’s not unusual.

So, I don’t need to understand the how - as per usual, smarter people have my back. But, am I safe to assume that since we are now referencing the home latitude, that it’s globally mobile and will function wherever HA knows its GPS coords?

And finally, there was talk of the sun’s elevation value being questionable. How much would this come into play with the new approach?
Markus, thanks so much, what a fascinating rabbit hole you pushed me into. :smiley:

Thanks, John

Yes exactly!
The max_elevation calculation uses some Fourier series thing. It only needs the day of the year (N) and the latitude (L). The first thing determines the Sun’s angle to the Earth’s axis (depends of N) and it needs to be modified because of the local position’s latitude.
It was the hard part. :slight_smile:

Yeah, but that “magic” is just a linear transformation to stretch the Sun’s elevation angle to the predefined values of mired_min and mired_max. It’s really a very simple approximation, but I think it’s pretty close to the real values and it’s enough for me.

I think, yes. But I tested it only here in Hungary, so I can’t be sure. :slight_smile:
The second part doesn’t depend on your location and the first part you can easily check, because sun.sun shows the elevation too and at noon you can take a look to compare the values. It’s not exactly the same but I think it’s because of the Fourier series and the difference is really small.

To tell the truth I have no idea. :smiley:
I compared my method’s results to @mastermarkush’s project and it was so close that I can’t see the difference in the mireds with my eyes. This is a simple idea but maybe it works. I don’t know for sure because I may be the only one who tried this little template sensor of mine. :sweat_smile:

1 Like