Platform Home Assistant "Template Sensor”

2 minutes of reading
Proconductor: Home Assistant Community
Availability: included in thepersonale HUB Home Assistant
Category: software
Type: platform Home Assistant
Family: component "Sensor” Home Assistant
Implementation difficulties: average

The platform "Template Sensor”, daughter of the component "Sensor” di Home Assistant, serves to define in configuration one or more sensors able prolast readings deriving from others entity, from simple, conditional calculations, even from arbitrary assumptions: apptogether, from template.

Configuration

the configuration basis of any "Template sensor" in the file configuration.yaml it's pretty simple.
It is sufficient to insert an entry of this type:

# Esempio di entry su configuration.yaml
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 }}"

The variables available for configuration customization are the following:

friendly_name(string, optional) the name displayed on the frontend
friendly_name_template(string, optional) the name displayed on the frontend generated through the template (overwrites any "friendly_name")
entity_id(string | list, optional) the list of entity IDs with respect to which the sensor reacts only to status changes. This can be used if the automatic analysis cannot find all the relevant entities.
unit_of_measurement(string, optional) defines the possible unit of measurement of the sensor. In the absence, it will directly show the discrete values ​​from it products.
value_template(template, required) defines the template that defines the sensor status.
icon_template(template, optional) defines the template that defines the sensor icon.
entity_picture_template(template, optional) defines the template that defines the image of the sensor.
device_class(class, optional) defines the type / class of the sensor to determine its correct automatic icon at the frontend.

Considerations

In case the sensor "source" procome from a platform that, at the start of Home Assistant, foresee time to be executed and evaluated, the status of the template sensor could initially obtain value "unknown". To avoid this (and the resulting error in the log file) you can use the is_state () function within the template itself.
For example, you could replace a type template

{{ states.switch.source.state == 'on' }}

in

{{is_state('switch.source', 'on') }}

which always returns a state true | falseMore unknown.

Examples

DETERMINATION OF THE SOLAR ANGLE

The following example produce a sensor that indicates the angle of the sun in the sky:

sensor:
  - platform: template
    sensors:
      solar_angle:
        friendly_name: "Angolo del sole"
        unit_of_measurement: '°'
        value_template: "{{ '%+.1f'|format(states.sun.sun.attributes.elevation) }}"
RENAME THE SENSOR OUTPUT

It is possible to arbitrarily determine the sensor output using specific strings.
So let's configure a sensor that responds, for example "High | low" based on whether the sun is above or below the horizon:

sensor:
  - platform: template
    sensors:
      sun_state:
        friendly_name: "Stato del sole"
        value_template: >-
          {% if is_state('sun.sun', 'above_horizon') %}
            alto
          {% else %}
            basso
          {% endif %}
MULTIPLE CONDITIONS

It is also possible to configure templates with multiple conditions:

sensor:
  - platform: template
    sensors:
      presa_bagno:
        friendly_name: "Presa del bagno"
        value_template: >-
          {% if is_state('switch.kettle', 'off') %}
            spento
          {% elif states.switch.kettle.attributes.kwh|float < 1000 %}
            standby
          {% elif is_state('switch.kettle', 'on') %}
            acceso
          {% else %}
            fallito
          {% endif %}

      next_sensor:
        ...

An even more complete list of examples is available on the component page presso Home Assistant.


inDomus telegram channel