Template Sensor


The template platform supports sensors which breaks 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:
      value_template: '{{ states.sun.sun.attributes.elevation }}'
      friendly_name: 'Sun angle'
      unit_of_measurement: 'degrees'
    sunrise:
      value_template: '{{ states.sun.sun.attributes.next_rising }}'

Configuration variables:

  • sensors array (Required): List of your sensors.
    • friendly_name (Optional): Name to use in the Frontend.
    • unit_of_measurement (Optional): Defines the units of measurement of the sensor, if any.
    • value_template (Optional): Defines a template to extract a value from the payload.
    • warnings (Optional): Turn off warnings (useful if the sensor is loaded before devices it depends on).
    • entity_id (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 it’s state.

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:
        value_template: '{{ "%+.1f"|format(states.sun.sun.attributes.elevation) }}'
        friendly_name: 'Sun Angle'
        unit_of_measurement: '°'

Multi line example with an if test (and warnings disabled)

This example shows a multiple line template with and if test. It looks at a sensing switch and shows on/off in the frontend. It disables warnings to avoid log messages where the switch it depends on isn’t loaded yet.

sensor:
  platform: template
  sensors:
      kettle:
        friendly_name: 'Kettle'
        value_template: >-
            {%- if is_state("switch.kettle", "off") %}
                off
            {%  elif states.switch.kettle.attributes.kwh < 1000 %}
                standby
            {% elif is_state("switch.kettle", "on") %}
                on
            {% else %}
                failed
            {%- endif %}

       warnings: Off

(please note the blank line to close the multi-line template)

Change the unit of measurment

With a template sensor it’s easy to convert given values into others if the unit of measurement don’t fit your needs.

sensor:
  platform: template
  sensors:
    transmission_down_speed_kbps:
        value_template: '{{ states.sensor.transmission_down_speed.state | multiply(1024) }}'
        friendly_name: 'Transmission Down Speed'
        unit_of_measurement: 'kB/s'
    transmission_up_speed_kbps:
        value_template: '{{ states.sensor.transmission_up_speed.state | multiply(1024) }}'
        friendly_name: 'Transmission Up Speed'
        unit_of_measurement: 'kB/s'