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
This commit is contained in:
NovapaX 2018-02-12 04:51:04 +01:00 committed by Alok Saboo
parent f566a97641
commit cda6c85186

View File

@ -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 %}