Problem with sensor for dusk to dawn timing

Hi,

I am trying to create a template sensor which detects Dawn, Day, Dusk, Night, Late Night and changes state, icon, icon colour. I am using CustomUI and have been researching this for the last few days and am at a loss on how to get it work.

packages/lighting/light_sensors.yaml

sensor:
  - platform: template
    sensors:
      day_night:
        friendly_name: "Day/Night"
        unique_id: day_night
        state: >
          {% set elevation = state_attr('sun.sun', 'elevation') %}
          {% set rising = state_attr('sun.sun', 'rising') %}
          {%- if elevation <= -2 & rising = false -%}
            Dusk
          {%- if elevation <= -50 & rising = false -%}
            Night
          {%- if elevation <= -60 & rising = false -%}
            Late Night
          {%- elif elevation > -2 & rising = true -%}
            Dawn
          {%- else -%}
            Day
          {%- endif -%}
        icon: >
          {% set elevation = state_attr('sun.sun', 'elevation') %}
          {% set rising = state_attr('sun.sun', 'rising') %}
          {%- if elevation <= -2 & rising = false -%}
            mdi:weather-sunset-down
          {%- if elevation <= -50 & rising = false -%}
            mdi:weather-night
          {%- if elevation <= -60 & rising = false -%}
            mdi:weather-night
          {%- elif elevation => -2 & rising = true -%}
            mdi:weather-sunset-up
          {%- else -%}
            mdi:weather-sunny
          {%- endif -%}
        attributes:
          icon_color: >
            {% set elevation = state_attr('sun.sun', 'elevation') %}
            {% set rising = state_attr('sun.sun', 'rising') %}
            {%- if elevation <= -2 & rising = false -%}
              rgb(217, 143, 0)
            {%- if elevation <= -50 & rising = false -%}
              rgb(9, 38, 184)
            {%- if elevation <= -60 & rising = false -%}
              rgb(39,38,84)
            {%- elif elevation => -2 & rising = true -%}
              rgb(255, 168, 0)
            {%- else -%}
              rgb(255, 249, 0)
            {%- endif -%}

Error:

**2024-09-24 15:24:19.458 ERROR (MainThread) [homeassistant.config] Invalid config for 'sensor' from integration 'template' at packages/lighting/light_sensors.yaml, line 13: required key 'value_template' not provided, please check the docs at https://www.home-assistant.io/integrations/template**

Invalid config for 'sensor' from integration 'template' at packages/lighting/light_sensors.yaml, line 16: 'state' is an invalid option for 'template.sensor', check: sensors->day_night->state, please check the docs at https://www.home-assistant.io/integrations/template

Invalid config for 'sensor' from integration 'template' at packages/lighting/light_sensors.yaml, line 30: 'icon' is an invalid option for 'template.sensor', check: sensors->day_night->icon, please check the docs at https://www.home-assistant.io/integrations/template

Invalid config for 'sensor' from integration 'template' at packages/lighting/light_sensors.yaml, line 44: 'attributes' is an invalid option for 'template.sensor', check: sensors->day_night->attributes, please check the docs at https://www.home-assistant.io/integrations/template`
  1. Use supported symbols and operators in your templates:

  2. Only one if is supported, use elif if you need multiple test statements. Jinja Docs - If statements

{% if  ....%}
{% elif  ....%}
{% else  ....%}
{% endif  ....%}
  1. The template needs more specific ranges and/or specific order. The template will return “Dusk” for the majority of the night before solar midnight. This is because of the broad range, and the order in which that if clause is tested. To define a more specific range you can do something like:
...
{%- if -18 < elevation <= -2 and not rising -%}
  Dusk
....
1 Like

Besides, you are mixing up new (integration) and legacy (platform) template sensor yaml.
Choose one or the other :wink:

2 Likes

Ok, I have made changes trying to follow

template:
  - sensor:
  ## Day/Night ##
    - name: "Day/Night"
      state: >
        {% set elevation = state_attr('sun.sun', 'elevation') %}
        {% set rising = state_attr('sun.sun', 'rising') %}
        {% if -2 < elevation <= 2 and not rising %}
          Dusk
        {% elif -50 < elevation <= -2 and not rising %}
          Night
        {% elif -60 < elevation <= -50 and not rising %}
          Late Night
        {% elif -2 > elevation => 2 and rising %}
          Dawn
        {% else %}
          Day
        {% endif %}
      icon: >
        {% set elevation = state_attr('sun.sun', 'elevation') %}
        {% set rising = state_attr('sun.sun', 'rising') %}
        {% if -2 < elevation <= 2 and not rising %}
          mdi:weather-sunset-down
        {% elif -50 < elevation <= -2 and not rising %}
          mdi:weather-night
        {% elif -60 < elevation <= -50 and not rising %}
          mdi:weather-night
        {% elif -2 > elevation => 2 and rising %}
          mdi:weather-sunset-up
        {% else %}
          mdi:weather-sunny
        {% endif %}
      attributes:
        icon_color: >
          {% set elevation = state_attr('sun.sun', 'elevation') %}
          {% set rising = state_attr('sun.sun', 'rising') %}
          {% if -2 < elevation <= 2 and not rising %}
            rgb(217, 143, 0)
          {% elif -50 < elevation <= -2 and not rising %}
            rgb(9, 38, 184)
          {% elif -60 < elevation <= -50 and not rising %}
            rgb(39,38,84)
          {% elif -2 > elevation => 2 and rising %}
            rgb(255, 168, 0)
          {% else %}
            rgb(255, 249, 0)
          {% endif %}

and now getting the error:

2024-09-27 23:26:04.010 ERROR (MainThread) [homeassistant.config] Invalid config for 'template' at packages/lighting/light_sensors.yaml,
line 13: invalid template (TemplateSyntaxError: unexpected '=') for dictionary value 'sensor->0->state', got
"{% set elevation = state_attr('sun.sun', 'elevation') %}
{% set rising = state_attr('sun.sun', 'rising') %}
{% if -2 < elevation <== 2 and not rising %
Dusk
{% elif -50 < elevation <== -2 and not rising %
Night
{% elif -60 < elevation <== -50 and not rising %}
Late Night
{% elif -2 > elevation ==> 2 and rising %}
Dawn
{% else %}
Day
{% endif %}"

log:

Invalid config for 'template' at packages/lighting/light_sensors.yaml,
line 27: invalid template (TemplateSyntaxError: unexpected '=') for dictionary value 'sensor->0->icon', got
"{% set elevation = state_attr('sun.sun', 'elevation') %}
{% set rising = state_attr('sun.sun', 'rising') %}
{% if -2 < elevation <== 2 and not rising %}
mdi:weather-sunset-down
{% elif -50 < elevation <== -2 and not rising %}
mdi:weather-night
{% elif -60 < elevation <== -50 and not rising %}
mdi:weather-night
{% elif -2 > elevation ==> 2 and rising %}
mdi:weather-sunset-up
{% else %}
mdi:weather-sunny
{% endif %}"

Invalid config for 'template' at packages/lighting/light_sensors.yaml,
line 42: invalid template (TemplateSyntaxError: unexpected '=') for dictionary value 'sensor->0->attributes->icon_color', got
"{% set elevation = state_attr('sun.sun', 'elevation') %}
{% set rising = state_attr('sun.sun', 'rising') %}
{% if -2 < elevation <== 2 and not rising %}
rgb(217, 143, 0)
{% elif -50 < elevation <== -2 and not rising %}
rgb(9, 38, 184)
{% elif -60 < elevation <== -50 and not rising %}
rgb(39,38,84)
{% elif -2 > elevation ==> 2 and rising %}
rgb(255, 168, 0)
{% else %}
gb(255, 249, 0)
{% endif %}"

Jinja Docs - Comparison symbols

1 Like

Read the error message, and check the code you’re posting is live. The error is moaning about your incorrect use of <== and ==>, but these symbols don’t appear in the code you’ve posted above.

Per Drew’s link, you want >= (greater than or equal to) and <= (less that or equal to).

I have fixed it, and the sensor is now appearing in Helpers.
Its state is now reporting as Day.

Attributes:
Icon color rgb(255, 249, 0)

The icon colour is not changing though.

image

My current time is 00:21

Can you point to the documentation that explains how your icon_color attribute works?

I’m using Custom UI and Custom Icon Color

Sorry it’s taken me so long to reply. Been busy with work. I did end up going through the code and modifying to reflect the integration template sensor jinja.

Seeing as I can’t mark multiple solutions. Thanks everyone for your help. I have the template sensor working now. My next step is to figure out templating for input booleans.