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`