loovanloon
(Ton van Loon)
December 15, 2023, 12:10pm
1
I want to create a template sensor that has a state and has its own sensor attributes. I would like to get some help.
Futhermore: The sensor and its attributes should have no values at Home Assistant reboot / restart.
I have a templates.yaml that holds a lot of working template sensors.
So far I have come up with this code in my templates.yaml:
- sensor:
name: Laatste Deconz Tijden
icon: mdi:weather-sunny
state: none
attribute_templates:
nadir: none
night_end: none
nautical_dawn: none
dawn: none
sunrise_start: none
sunrise_end: none
golden_hour_1: none
solar_noon: none
golden_hour_2: none
sunset_start: none
sunset_end: none
dusk: none
nautical_dusk: none
night_start: none
I get a config error message:
Error loading /config/configuration.yaml: mapping values are not allowed here
in "/config/template_sensors.yaml", line 1115, column 26
Line 1115 is the line starting with: attribute_templates:
column 26 is the ":ā
So I guess this is not the way to create attributes,
Background info:
My goal is to store the times when my ādeconz time of day sensorā changes to a different ādeconz time of dayā This will be done with an automation that is already running for other purposes also. As state it will have the time of the last change.
hawksj
(Sam)
December 15, 2023, 12:22pm
2
loovanloon:
attribute_templates:
Have you checked the template documentation ? attribute_templates
is not a valid key. You define attributes under the attributes
variable which should be on the same level as state
(not indented beneath it).
The linked documentation includes examples of template attributes at the bottom of the page.
1 Like
loovanloon
(Ton van Loon)
December 15, 2023, 12:46pm
3
I checked the docs, but overlooked it. My bad. Thank you. Now this code works.
- sensor:
name: Laatste Deconz Tijden
icon: mdi:weather-sunny
state: none
attributes:
nadir: none
night_end: none
nautical_dawn: none
dawn: none
sunrise_start: none
sunrise_end: none
golden_hour_1: none
solar_noon: none
golden_hour_2: none
sunset_start: none
sunset_end: none
dusk: none
nautical_dusk: none
night_start: none
2 Likes
hawksj
(Sam)
December 15, 2023, 1:31pm
4
Good job, Iāll be honest I actually didnāt know this was possible until I looked more in-depth at the template docs to answer this post. I think Iāll start implementing this myself now!
1 Like
loovanloon
(Ton van Loon)
December 16, 2023, 5:23pm
5
Since updating the attribute with an automation does not work, I changed the template sensor into a State Triggered Template Sensor with attributes.
For this I have read the following documentation and help posts:
The following code now works fine for me:
# DECONZ: LATEST TIMES OF CHANGES DECONZ SUN PHASES
- trigger:
# WHEN EVENT 'sensor.deconz_daylight' changes state THEN update the attributes of 'sensor.deconz_daylight_latest_times' CONDITIONALLY:
# - IF sensor.laatste_deconz_tijden.state == sensor.deconz_daylight_latest_times.[attribute_name] THEN :
# >> Store the current time to the [attribute_name] that matches the changed new state of 'sensor.deconz_daylight'
# - IF sensor.laatste_deconz_tijden.state != sensor.deconz_daylight_latest_times.[attribute_name] THEN:
# >> Re-store the old value of the attribute from the stored value in its respective m_* attribute variable
- platform: state
entity_id:
- sensor.deconz_daylight
action: []
sensor:
name: Deconz Daylight Latest Times
icon: mdi:weather-sunny
state: >-
{{ now().strftime('%d-%m-%Y om %H:%M:%S') }}
attributes:
datum: >-
{{ now().strftime('%d-%m-%Y') }}
nadir: >-
{% set m_nadir=state_attr('sensor.deconz_daylight_latest_times','nadir') %}
{% if states('sensor.deconz_daylight')=='nadir' %} {{ as_timestamp(now()) }} {% else %} {{ m_nadir }} {% endif %}
night_end: >-
{% set m_night_end=state_attr('sensor.deconz_daylight_latest_times','night_end') %}
{% if states('sensor.deconz_daylight')=='night_end' %} {{ as_timestamp(now()) }} {% else %} {{ m_night_end }} {% endif %}
nautical_dawn: >-
{% set m_nautical_dawn=state_attr('sensor.deconz_daylight_latest_times','nautical_dawn') %}
{% if states('sensor.deconz_daylight')=='nautical_dawn' %} {{ as_timestamp(now()) }} {% else %} {{ m_nautical_dawn }} {% endif %}
dawn: >-
{% set m_dawn=state_attr('sensor.deconz_daylight_latest_times','dawn') %}
{% if states('sensor.deconz_daylight')=='dawn' %} {{ as_timestamp(now()) }} {% else %} {{ m_dawn }} {% endif %}
sunrise_start: >-
{% set m_sunrise_start=state_attr('sensor.deconz_daylight_latest_times','sunrise_start') %}
{% if states('sensor.deconz_daylight')=='sunrise_start' %} {{ as_timestamp(now()) }} {% else %} {{ m_sunrise_start }} {% endif %}
sunrise_end: >-
{% set m_sunrise_end=state_attr('sensor.deconz_daylight_latest_times','sunrise_end') %}
{% if states('sensor.deconz_daylight')=='sunrise_end' %} {{ as_timestamp(now()) }} {% else %} {{ m_sunrise_end }} {% endif %}
golden_hour_1: >-
{% set m_golden_hour_1=state_attr('sensor.deconz_daylight_latest_times','golden_hour_1') %}
{% if states('sensor.deconz_daylight')=='golden_hour_1' %} {{ as_timestamp(now()) }} {% else %} {{ m_golden_hour_1 }} {% endif %}
solar_noon: >-
{% set m_solar_noon=state_attr('sensor.deconz_daylight_latest_times','solar_noon') %}
{% if states('sensor.deconz_daylight')=='solar_noon' %} {{ as_timestamp(now()) }} {% else %} {{ m_solar_noon }} {% endif %}
golden_hour_2: >-
{% set m_golden_hour_2=state_attr('sensor.deconz_daylight_latest_times','golden_hour_2') %}
{% if states('sensor.deconz_daylight')=='golden_hour_2' %} {{ as_timestamp(now()) }} {% else %} {{ m_golden_hour_2 }} {% endif %}
sunset_start: >-
{% set m_sunset_start=state_attr('sensor.deconz_daylight_latest_times','sunset_start') %}
{% if states('sensor.deconz_daylight')=='sunset_start' %} {{ as_timestamp(now()) }} {% else %} {{ m_sunset_start }} {% endif %}
sunset_end: >-
{% set m_sunset_end=state_attr('sensor.deconz_daylight_latest_times','sunset_end') %}
{% if states('sensor.deconz_daylight')=='sunset_end' %} {{ as_timestamp(now()) }} {% else %} {{ m_sunset_end }} {% endif %}
dusk: >-
{% set m_dusk=state_attr('sensor.deconz_daylight_latest_times','dusk') %}
{% if states('sensor.deconz_daylight')=='dusk' %} {{ as_timestamp(now()) }} {% else %} {{ m_dusk }} {% endif %}
nautical_dusk: >-
{% set m_nautical_dusk=state_attr('sensor.deconz_daylight_latest_times','nautical_dusk') %}
{% if states('sensor.deconz_daylight')=='nautical_dusk' %} {{ as_timestamp(now()) }} {% else %} {{ m_nautical_dusk }} {% endif %}
night_start: >-
{% set m_night_start=state_attr('sensor.deconz_daylight_latest_times','night_start') %}
{% if states('sensor.deconz_daylight')=='night_start' %} {{ as_timestamp(now()) }} {% else %} {{ m_night_start }} {% endif %}
3 Likes
How can this be achieved using the system UI to create a template sensor? Since templates are about to/ have been removed from config files.
1 Like
hawksj
(Sam)
June 24, 2024, 8:15pm
7
It doesnāt appear to be possible through the UI at this time. What is your source for YAML templates being unsupported? Iāve not been very active in the community and may well have missed it but the HA Devs donāt tend to fully remove YAML functionality unless you can achieve the same result with what is already available in the UI. My YAML template sensors are still working perfectly.
retrography
(Mahmood S. Zargar)
July 17, 2024, 1:06pm
8
The old format template sensors are deprecated and ought to be removed, and not the templates altogether. New format template sensors remain valid (under template section rather than the sensor section).
marknl
(Mark)
August 3, 2024, 10:50am
9
Hi everyone Iām new to home-assistent and was looking for a way to add these sun times to my config.
I guess I should add both items from @loovanloon to my templates.yaml?
In configuration.yaml I added template: !include templates.yaml
Now I am trying to figure out how to turn on a switch at the start of the golden hour.
Is any of the code out of date? Iām having a bit of trouble following that part of the discussion, so please enlighten me, thank you.
MEC88
August 29, 2024, 2:40pm
10
Hi all,
i have a quiet similar problem.
I want to create a template sensor like the existing āsensor.solcast_pv_forecast_prognose_heuteā (via the Integration āSolcast PV Forecastā):
My need ist to calculate the values of the Attributes in Order to get a sensor, which Shows my āestimated battery Socā. I want to display it with Apex Chart Card.
So i could see, wether and when battery SOC is going low ā charging from grid at the time of lowest prices.
Would be nice, if anyone can help me ā¦