Skip to content

Commit 783d4db

Browse files
amelchiofrenck
authored andcommitted
Cleanup descriptions for template sensor/binary_sensor (home-assistant#7585)
1 parent 278f646 commit 783d4db

File tree

2 files changed

+39
-62
lines changed

2 files changed

+39
-62
lines changed

source/_components/binary_sensor.template.markdown

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@ logo: home-assistant.png
1414
ha_qa_scale: internal
1515
---
1616

17-
The `template` platform supports sensors which break out the `state` and
18-
`state_attributes` from other entities. The state of a Template Binary Sensor
19-
can only be `on` or `off`.
17+
The `template` platform supports binary sensors which get their values from
18+
other entities. The state of a Template Binary Sensor can only be `on` or
19+
`off`.
2020

2121
## {% linkable_title Configuration %}
2222

23-
To enable Template Binary Sensors in your installation, add the following to
24-
your `configuration.yaml` file:
23+
Here is an example of adding a Template Binary Sensor to the `configuration.yaml` file:
2524

2625
{% raw %}
2726
```yaml
@@ -84,6 +83,8 @@ sensors:
8483

8584
## {% linkable_title Considerations %}
8685

86+
### Startup
87+
8788
If you are using the state of a platform that takes extra time to load, the
8889
Template Binary Sensor may get an `unknown` state during startup. This results
8990
in error messages in your log file until that platform has completed loading.
@@ -94,6 +95,14 @@ with this equivalent that returns `true`/`false` and never gives an unknown
9495
result:
9596
{% raw %}`{{ is_state('switch.source', 'on') }}`{% endraw %}
9697

98+
### Entity IDs
99+
100+
The template engine will attempt to work out what entities should trigger an
101+
update of the sensor. This can fail, for example if your template loops over
102+
the contents of a group. In this case you can use `entity_id` to provide a
103+
list of entity IDs that will cause the sensor to update or you can run the
104+
service `homeassistant.update_entity` to update the sensor at will.
105+
97106
## {% linkable_title Examples %}
98107

99108
In this section you find some real-life examples of how to use this sensor.

source/_components/sensor.template.markdown

Lines changed: 25 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,11 @@ logo: home-assistant.png
1414
ha_qa_scale: internal
1515
---
1616

17-
The `template` platform supports sensors which break out `state_attributes` from other entities.
18-
19-
<p class='note'>
20-
If you do not supply an `entity_id` in the configuration you will need to run the service `homeassistant.update_entity` to update the sensor.
21-
</p>
17+
The `template` platform supports sensors which get their values from other entities.
2218

2319
## {% linkable_title Configuration %}
2420

25-
To enable Template Sensors in your installation, add the following to your `configuration.yaml` file:
21+
The configuration of Template Sensors depends on what you want them to be. Adding the following to your `configuration.yaml` file will create two sensors, one for the current sun angle and one for the time of the next sunrise:
2622

2723
{% raw %}
2824
```yaml
@@ -55,7 +51,7 @@ sensor:
5551
required: false
5652
type: template
5753
entity_id:
58-
description: The template engine will attempt to work out what entities should trigger an update of the sensor. If this fails to get the correct list (for example if your template loops over the contents of a group) then you can provide a list of entity IDs that will cause the sensor to update.
54+
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.
5955
required: false
6056
type: string, list
6157
unit_of_measurement:
@@ -84,9 +80,15 @@ sensor:
8480

8581
## {% linkable_title Considerations %}
8682

83+
### Startup
84+
8785
If you are using the state of a platform that takes extra time to load, the Template Sensor may get an `unknown` state during startup. To avoid this (and the resulting error messages in your log file), you can use `is_state()` function in your template. For example, you would replace {% raw %}`{{ states.switch.source.state == 'on' }}`{% endraw %} with this equivalent that returns `true`/`false` and never gives an `unknown` result:
8886
{% raw %}`{{ is_state('switch.source', 'on') }}`{% endraw %}
8987

88+
### Entity IDs
89+
90+
The template engine will attempt to work out what entities should trigger an update of the sensor. This can fail, for example if your template loops over the contents of a group. In this case you can use `entity_id` to provide a list of entity IDs that will cause the sensor to update or you can run the service `homeassistant.update_entity` to update the sensor at will.
91+
9092
## {% linkable_title Examples %}
9193

9294
In this section you find some real-life examples of how to use this sensor.
@@ -128,26 +130,6 @@ sensor:
128130
```
129131
{% endraw %}
130132

131-
Processes monitored by the [System Monitor sensor](/components/sensor.systemmonitor/) show `on` or `off` if they are running or not. This example shows how the output of a monitored `glances` process can be renamed.
132-
133-
{% raw %}
134-
```yaml
135-
sensor:
136-
- platform: template
137-
sensors:
138-
glances:
139-
friendly_name: "Glances"
140-
value_template: >-
141-
{% if is_state('sensor.process_glances', 'on') %}
142-
running
143-
{% else %}
144-
not running
145-
{% endif %}
146-
```
147-
{% endraw %}
148-
149-
The [Template Binary Sensor](/components/binary_sensor.template/) is the one in similar cases if you prefer to see an icon instead of text.
150-
151133
### {% linkable_title Multiline Example With an `if` Test %}
152134

153135
This example shows a multiple line template with an `if` test. It looks at a sensing switch and shows `on`/`off` in the frontend.
@@ -169,9 +151,6 @@ sensor:
169151
{% else %}
170152
failed
171153
{% endif %}
172-
173-
next_sensor:
174-
...
175154
```
176155
{% endraw %}
177156

@@ -250,27 +229,6 @@ sensor:
250229

251230
### {% linkable_title Change the Friendly Name Used in the Frontend %}
252231

253-
This example shows how to change the `friendly_name` based on a date.
254-
Explanation: We add a multiple of 86400 seconds (= 1 day) to the current unix timestamp to get a future date.
255-
256-
{% raw %}
257-
```yaml
258-
sensor:
259-
- platform: template
260-
sensors:
261-
forecast_1_day_ahead:
262-
friendly_name_template: >-
263-
{%- set date = as_timestamp(now()) + (1 * 86400 ) -%}
264-
{{ date|timestamp_custom("Tomorrow (%-m/%-d)") }}
265-
value_template: "{{ sensor.darksky_weather_forecast_1 }}"
266-
forecast_2_days_ahead:
267-
friendly_name_template: >-
268-
{%- set date = as_timestamp(now()) + (2 * 86400 ) -%}
269-
{{ date|timestamp_custom("%A (%-m/%-d)") }}
270-
value_template: "{{ sensor.darksky_weather_forecast_2 }}"
271-
```
272-
{% endraw %}
273-
274232
This example shows how to change the `friendly_name` based on a state.
275233

276234
{% raw %}
@@ -290,9 +248,11 @@ sensor:
290248
```
291249
{% endraw %}
292250

293-
### {% linkable_title Working with dates %}
251+
### {% linkable_title Working without entities %}
252+
253+
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).
294254

295-
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). This template contains no entities that will trigger an update, so either we need to use `homeassistant.update_entity` or add an `entity_id:` line for an entity that will force an update - here we're using `sensor.date`.
255+
This template contains no entities that will trigger an update, so we add an `entity_id:` line with an entity that will force an update - here we're using a [date sensor](/components/sensor.time_date/) to get a daily update:
296256

297257
{% raw %}
298258
```yaml
@@ -309,17 +269,25 @@ sensor:
309269

310270
Useful entities to choose might be `sensor.date` which update once per day, or `sensor.time` which updates once per minute.
311271

312-
Note: [Time & Date Sensors](https://www.home-assistant.io/components/sensor.time_date/) used as an update trigger, must be configured. If a template uses more than one sensor they can be listed.
313-
314-
The alternative to this is to create an `Automation`using the new (81.0) service `homeassistant.update_entity` and list all entity's requiring updates and setting the interval based on time.
272+
An alternative to this is to create an interval-based automation that calls the service `homeassistant.update_entity` for the entities requiring updates. This modified example updates every 5 minutes:
315273

316274
{% raw %}
317275
```yaml
276+
sensor:
277+
- platform: template
278+
sensors:
279+
nonsmoker:
280+
value_template: '{{ (( as_timestamp(now()) - as_timestamp(strptime("06.07.2018", "%d.%m.%Y")) ) / 86400 ) | round(2) }}'
281+
entity_id: []
282+
friendly_name: 'Not smoking'
283+
unit_of_measurement: "Days"
284+
318285
automation:
319286
- alias: 'nonsmoker_update'
320287
trigger:
321288
- platform: time
322-
minutes: '/1'
289+
minutes: '/5'
290+
seconds: 0
323291
action:
324292
- service: homeassistant.update_entity
325293
entity_id: sensor.nonsmoker

0 commit comments

Comments
 (0)