Template Sensor
The template
platform supports sensors which break out state_attributes
from other entities.
To enable Template Sensors in your installation, add the following to your
configuration.yaml
file:
# Example configuration.yaml entry
sensor:
- platform: template
sensors:
solar_angle:
friendly_name: "Sun angle"
unit_of_measurement: 'degrees'
value_template: "{{ states.sun.sun.attributes.elevation }}"
sunrise:
value_template: "{{ states.sun.sun.attributes.next_rising }}"
Configuration Variables
- sensors
-
(map)(Required)List of your sensors.
-
- friendly_name
-
(string)(Optional)Name to use in the frontend.
- entity_id
-
(string | list)(Optional)Add a list of entity IDs so the sensor only reacts to state changes of these entities. This will reduce the number of times the sensor will try to update its state.
- unit_of_measurement
-
(string)(Optional)Defines the units of measurement of the sensor, if any.
- value_template
-
(template)(Required)Defines a template to get the state of the sensor.
- icon_template
-
(template)(Optional)Defines a template for the icon of the sensor.
Considerations
If you are using the state of a platform that takes extra time to load, the
Template Sensor may get an unknown
state during startup. This results
in error messages in your log file until that platform has completed loading.
If you use is_state()
function in your template, you can avoid this situation.
For example, you would replace
{{ states.switch.source.state == 'on' }}
with this equivalent that returns true
/false
and never gives an unknown
result:
{{ is_state('switch.source', 'on') }}
Examples
In this section you find some real life examples of how to use this sensor.
Sun Angle
This example shows the sun angle in the frontend.
sensor:
- platform: template
sensors:
solar_angle:
friendly_name: "Sun Angle"
unit_of_measurement: '°'
value_template: "{{ '%+.1f'|format(states.sun.sun.attributes.elevation) }}"
Renaming Sensor Output
If you don’t like the wording of a sensor output then the Template Sensor can help too. Let’s rename the output of the Sun component as a simple example:
sensor:
- platform: template
sensors:
sun_state:
friendly_name: "Sun State"
value_template: >-
{% if is_state('sun.sun', 'above_horizon') %}
up
{% else %}
down
{% endif %}
Processes monitored by the System Monitor sensor
show on
or off
if they are running or not. This example shows how the
output of a monitored glances
process can be renamed.
sensor:
- platform: template
sensors:
glances:
friendly_name: "Glances"
value_template: >-
{% if is_state('sensor.process_glances', 'on') %}
running
{% else %}
not running
{% endif %}
The Template Binary Sensor is the one in similar cases if you prefer to see an icon instead of text.
Multiline Example With an if
Test
This example shows a multiple line template with an if
test. It looks at a
sensing switch and shows on
/off
in the frontend.
sensor:
- platform: template
sensors:
kettle:
friendly_name: "Kettle"
value_template: >-
{% if is_state('switch.kettle', 'off') %}
off
{% elif states.switch.kettle.attributes.kwh|float < 1000 %}
standby
{% elif is_state('switch.kettle', 'on') %}
on
{% else %}
failed
{% endif %}
next_sensor:
...
Change The Unit of Measurement
With a Template Sensor it’s easy to convert given values into others if the unit of measurement doesn’t fit your needs.
sensor:
- platform: template
sensors:
transmission_down_speed_kbps:
friendly_name: "Transmission Down Speed"
unit_of_measurement: 'kB/s'
value_template: "{{ states('sensor.transmission_down_speed')|float * 1024 }}"
transmission_up_speed_kbps:
friendly_name: "Transmission Up Speed"
unit_of_measurement: 'kB/s'
value_template: "{{ states('sensor.transmission_up_speed')|float * 1024 }}"
Change The Icon
This example shows how to change the icon based on the day/night cycle.
sensor:
- platform: template
sensors:
day_night:
friendly_name: "Day/Night"
value_template: >-
{% if is_state('sun.sun', 'above_horizon') %}
Day
{% else %}
Night
{% endif %}
icon_template: >-
{% if is_state('sun.sun', 'above_horizon') %}
mdi:weather-sunny
{% else %}
mdi:weather-night
{% endif %}