diff --git a/source/_components/template.markdown b/source/_components/template.markdown index cf0155e14b2..17367c9e03a 100644 --- a/source/_components/template.markdown +++ b/source/_components/template.markdown @@ -68,6 +68,15 @@ sensor: description: Defines a template for the entity picture of the sensor. required: false type: template + attribute_templates: + description: Defines templates for attributes of the sensor. + required: false + type: map + keys: + "attribute: template": + description: The attribute and corresponding template. + required: true + type: template device_class: description: Sets the class of the device, changing the device state and icon that is displayed on the UI (see below). It does not set the `unit_of_measurement`. required: false @@ -246,6 +255,38 @@ sensor: ``` {% endraw %} +### Add Custom Attributes + +This example shows how to add custom attributes. + +{% raw %} +```yaml +sensor: + - platform: template + sensors: + my_device: + value_template: >- + {% if is_state('device_tracker.my_device_nmap','home') %} + Home + {% else %} + {{ states('device_tracker.my_device_gps') }} + {% endif %} + attribute_templates: + latitude: >- + {% if is_state('device_tracker.my_device_nmap','home') %} + {{ state_attr('zone.home','latitude') }} + {% else %} + state_attr('device_tracker.my_device_gps','latitude') + {% endif %} + longitude: >- + {% if is_state('device_tracker.my_device_nmap','home') %} + {{ state_attr('zone.home','longitude') }} + {% else %} + {{ state_attr('device_tracker.my_device_gps','longitude') }} + {% endif %} +``` +{% endraw %} + ### Working without entities The `template` sensors are not limited to use attributes from other entities but can also work with [Home Assistant's template extensions](/docs/configuration/templating/#home-assistant-template-extensions).