Skip to content

[Enhancement] Account for Sun-Window angle for sun triggers #187

Open
@itsamejoshab

Description

@itsamejoshab

With my blinds, I found the sun elevation itself to be inadequate to control when blinds should trigger. It depends on what way your window faces.

I have this code which you might find useful and incorporate into this blueprint.

You ask the user for 2 things:

  • win_azi the azimuth of the window
  • win_ele the elevation of the window
  • sun_ele_threshold the threshold of elevation where sun disappears at horizon

In this example I get the sun attributes from another integration, but the math to calculate my helper is all written out for you. win_azi I got from google earth.

        {% set deg2rad = pi/180 %}

        {% set sun_azi = state_attr('sun.sun', 'azimuth') | int %}
        {% set sun_ele = state_attr('sun.sun', 'elevation') | int %}

        {% set sun_x = cos(sun_azi*deg2rad)*cos(sun_ele*deg2rad) %}
        {% set sun_y = sin(sun_azi*deg2rad)*cos(sun_ele*deg2rad) %}
        {% set sun_z = sin(sun_ele*deg2rad) %}

        {% set win_azi = 232.68 %}
        {% set win_ele = 0 %}
        {% set sun_ele_threshold = 5 %}

        {% set win_x = cos(win_azi*deg2rad)*cos(win_ele*deg2rad) %}
        {% set win_y = sin(win_azi*deg2rad)*cos(win_ele*deg2rad) %}
        {% set win_z = sin(win_ele*deg2rad) %}

        {% set dot = sun_x*win_x + sun_y*win_y + sun_z*win_z %}
        {% set norm_win = sqrt(win_x**2 + win_y**2 + win_z**2) %}
        {% set norm_sun = sqrt(sun_x**2 + sun_y**2 + sun_z**2) %}
        {% set cos_sim = dot/(norm_win*norm_sun) %}

        {% set ang_sim = 1 - acos(cos_sim)/pi %}
        {{ ((cos_sim * 100) | round(0)) if (sun_ele > sun_ele_threshold and cos_sim > 0 ) else 0 }}

This gives you a basis to measure how directly the sun is looking into the window. Higher means more direct.

Image

Image

Next, I use a binary sensor to compare this number to a threshold which a user would set, in my case I chose 30.

    - name: "Kitchen Window Sun"
      state: >
        {% if (states('sensor.sun_window_ang_similarity')|float(default=0.0) > 30) %}
            true
        {% else %}
            false
        {% endif %}

This was a very satisfying way to control the blinds, better than just elevation.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions