After repeatedly reading the documentation and topics on homeassistant templating I’m still somewhat lost. What I try to achieve is to create a sensor which has two values: solar state (or state of the day, like midnight, morning, evening, etc) and light state (like dusk, dawn, goldenhour etc). I’ve created a template-sensor (helper) which calculates the these states but now I need this sensor to be accessible in other parts of homeassistant, like automations. The template is as follows (OK, I realise much must be optimized and completed, and the macro which is defined is an overkill and the details are to be tuned) but it now creates (more or less) correct states :
{%- macro between() -%}
{## arg 4 must be inclusive, exclusive, exclusive_low, exclusive_high ##}
{%- if ( varargs|count == 3 or varargs|count == 4 ) and varargs[0] is number and varargs[1] is number and varargs[2] is number -%}
{%- if varargs|count == 3 -%}
{%- set compare_type = "inclusive" -%}
{%- elif varargs[3] == "inclusive"or varargs[3] == "exclusive" or varargs[3] == "exclusive_low" or varargs[3] == "exclusive_high" -%}
{%- set compare_type = varargs[3] -%}
{%- else -%}
{% set compare_type = "invalid" -%}
{%- endif -%}
{%- if (varargs[1] <= varargs[0] and varargs[2] >= varargs[0] and compare_type == "inclusive") or
(varargs[1] < varargs[0] and varargs[2] > varargs[0] and compare_type == "exclusive") or
(varargs[1] < varargs[0] and varargs[2] >= varargs[0] and compare_type == "exclusive_low") or
(varargs[1] <= varargs[0] and varargs[2] > varargs[0] and compare_type == "exclusive_high")
-%}
{{ true }}
{%- elif compare_type == "invalid" -%}
{{ "compare_type is INVALID" }}
{%- else -%}
{{ false }}
{%- endif -%}
{%- else -%}
{{ "parameter count not correct or not numeric" }}
{%- endif -%}
{%- endmacro -%}
{% if state_attr("sun.sun", "azimuth") < 180 and between(state_attr("sun.sun", "elevation"), -0.16 , 0.16, "exclusive_high") == true -%}
{% set state_sun = "sunrise " %}
{% elif state_attr("sun.sun", "azimuth") > 180 and between(state_attr("sun.sun", "elevation"), -0.16 , 0.16, "exclusive_high") == true -%}
{% set state_sun = "sunset" %}
{% elif between(state_attr("sun.sun", "azimuth"),180,180.25,"exclude_low") == "True" -%}
{% set state_sun = "midday" %}
{% elif state_attr("sun.sun", "azimuth") > 359.999 or state_attr("sun.sun", "azimuth") < 0.25 == "True" -%}
{% set state_sun = "midnight" %}
{% elif between(state_attr("sun.sun", "azimuth"),0,180) == "True" and state_attr("sun.sun", "elevation") < -0.16 -%}
{% set state_sun = "night" %}
{% elif state_attr("sun.sun", "azimuth") <= 180 and state_attr("sun.sun", "elevation") >= 0.16 -%}
{% set state_sun = "morning" %}
{% elif state_attr("sun.sun", "azimuth") > 180.25 and state_attr("sun.sun", "elevation") < -0.16 -%}
{% set state_sun = "evening" %}
{% elif state_attr("sun.sun", "azimuth") > 180.25 and state_attr("sun.sun", "elevation") > 0.16 -%}
{% set state_sun = "afternoon" %}
{% endif %}
{% if between(state_attr("sun.sun", "elevation"),-3.0,6.0) == "True" -%}
{% set state_light = "goldenhour" %}
{%- else -%}
{% set state_light = none %}
{%- endif -%}
Sun: {{ state }}; Light: {{ state_light }}
What I would like is to be able to access the solar and light-state in an automation as
state_attr(‘helper_solar_state’,‘state_sun’) and state_attr(‘helper_solar_state’,‘state_light’)
And of course the light states in this helper still must be completed with other states, like dusk, dawn, nautical dusk, etc…