From cda6c8518660ca93476de0e79449267df9272d3c Mon Sep 17 00:00:00 2001 From: NovapaX Date: Mon, 12 Feb 2018 04:51:04 +0100 Subject: [PATCH] Update template_sensor with friendly_name_template attribute and example (#4644) * add documentation and example for friendly_name_template * standard fixes * another example + no whitespace around pipes * move the dot * little example fix * Minor changes --- source/_components/sensor.template.markdown | 46 +++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/source/_components/sensor.template.markdown b/source/_components/sensor.template.markdown index c8d8e2cd0a0..534fe54e9f7 100644 --- a/source/_components/sensor.template.markdown +++ b/source/_components/sensor.template.markdown @@ -45,6 +45,10 @@ sensor: description: Name to use in the frontend. required: false type: string + friendly_name_template: + description: Defines a template for the name to be used in the frontend (this overrides friendly_name). + required: false + type: template entity_id: description: A list of entity IDs so the sensor only reacts to state changes of these entities. This can be used if the automatic analysis fails to find all relevant entities. required: false @@ -244,3 +248,45 @@ sensor: {% endif %} ``` {% endraw %} + +### {% linkable_title Change the Friendly Name Used in the Frontend %} + +This example shows how to change the `friendly_name` based on a date. +Explanation: we add a multiple of 86400 seconds (= 1 day) to the current unix timestamp to get a future date. + +{% raw %} +```yaml +sensor: + - platform: template + sensors: + forecast_1_day_ahead: + friendly_name_template: >- + {%- set date = as_timestamp(now()) + (1 * 86400 ) -%} + {{ date|timestamp_custom("Tomorrow (%-m/%-d)") }} + value_template: "{{ sensor.darksky_weather_forecast_1 }}" + forecast_2_days_ahead: + friendly_name_template: >- + {%- set date = as_timestamp(now()) + (2 * 86400 ) -%} + {{ date|timestamp_custom("%A (%-m/%-d)") }} + value_template: "{{ sensor.darksky_weather_forecast_2 }}" +``` +{% endraw %} + +This example shows how to change the `friendly_name` based on a state. + +{% raw %} +```yaml +sensor: + - platform: template + sensors: + net_power: + friendly_name_template: >- + {% if states('sensor.power_consumption')|float < 0 %} + Power Consumption + {% else %} + Power Production + {% end %} + value_template: "{{ states('sensor.power_consumption') }}" + unit_of_measurement: 'kW' +``` +{% endraw %}