You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: source/_components/sensor.template.markdown
+25-57Lines changed: 25 additions & 57 deletions
Original file line number
Diff line number
Diff line change
@@ -14,15 +14,11 @@ logo: home-assistant.png
14
14
ha_qa_scale: internal
15
15
---
16
16
17
-
The `template` platform supports sensors which break out `state_attributes` from other entities.
18
-
19
-
<pclass='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.
22
18
23
19
## {% linkable_title Configuration %}
24
20
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:
26
22
27
23
{% raw %}
28
24
```yaml
@@ -55,7 +51,7 @@ sensor:
55
51
required: false
56
52
type: template
57
53
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.
59
55
required: false
60
56
type: string, list
61
57
unit_of_measurement:
@@ -84,9 +80,15 @@ sensor:
84
80
85
81
## {% linkable_title Considerations %}
86
82
83
+
### Startup
84
+
87
85
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:
88
86
{% raw %}`{{ is_state('switch.source', 'on') }}`{% endraw %}
89
87
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
+
90
92
## {% linkable_title Examples %}
91
93
92
94
In this section you find some real-life examples of how to use this sensor.
@@ -128,26 +130,6 @@ sensor:
128
130
```
129
131
{% endraw %}
130
132
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
-
151
133
### {% linkable_title Multiline Example With an `if` Test %}
152
134
153
135
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:
169
151
{% else %}
170
152
failed
171
153
{% endif %}
172
-
173
-
next_sensor:
174
-
...
175
154
```
176
155
{% endraw %}
177
156
@@ -250,27 +229,6 @@ sensor:
250
229
251
230
### {% linkable_title Change the Friendly Name Used in the Frontend %}
252
231
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 ) -%}
This example shows how to change the `friendly_name` based on a state.
275
233
276
234
{% raw %}
@@ -290,9 +248,11 @@ sensor:
290
248
```
291
249
{% endraw %}
292
250
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).
294
254
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:
296
256
297
257
{% raw %}
298
258
```yaml
@@ -309,17 +269,25 @@ sensor:
309
269
310
270
Useful entities to choose might be `sensor.date` which update once per day, or `sensor.time` which updates once per minute.
311
271
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:
0 commit comments