mirror of
https://github.com/home-assistant/home-assistant.io.git
synced 2025-07-15 13:26:54 +00:00
parent
89fdc593f0
commit
dfe9d13755
@ -17,11 +17,11 @@ To enable Template binary sensors in your installation, add the following to you
|
|||||||
```yaml
|
```yaml
|
||||||
# Example configuration.yaml entry
|
# Example configuration.yaml entry
|
||||||
binary_sensor:
|
binary_sensor:
|
||||||
platform: template
|
- platform: template
|
||||||
sensors:
|
sensors:
|
||||||
sun_up:
|
sun_up:
|
||||||
value_template: {% raw %}'{{ states.sun.sun.attributes.elevation > 0}}'{% endraw %}
|
value_template: {% raw %}'{{ states.sun.sun.attributes.elevation > 0}}'{% endraw %}
|
||||||
friendly_name: 'Sun is up'
|
friendly_name: 'Sun is up'
|
||||||
```
|
```
|
||||||
|
|
||||||
Configuration variables:
|
Configuration variables:
|
||||||
@ -30,8 +30,7 @@ Configuration variables:
|
|||||||
- **friendly_name** (*Optional*): Name to use in the Frontend.
|
- **friendly_name** (*Optional*): Name to use in the Frontend.
|
||||||
- **sensor_class** (*Optional*): The [type/class](/components/binary_sensor/) of the sensor to set the icon in the frontend.
|
- **sensor_class** (*Optional*): The [type/class](/components/binary_sensor/) of the sensor to set the icon in the frontend.
|
||||||
- **value_template** (*Optional*): Defines a [template](/topics/templating/) to extract a value from the payload.
|
- **value_template** (*Optional*): Defines a [template](/topics/templating/) to extract a value from the payload.
|
||||||
- **warnings** (*Optional*): Turn off warnings (useful if the sensor is loaded before devices it depends on).
|
- **entity_id** (*Optional*): Add a list of entity IDs so the sensor only reacts to state changes of these entities. This will reduce the number of times the sensor will try to update it's state.
|
||||||
- **entity_id** (*Optional*): Add a list of entity_ids so the sensor only reacts to state changes of these entities. This will reduce the number of times the sensor will try to update it's state.
|
|
||||||
|
|
||||||
## {% linkable_title Examples %}
|
## {% linkable_title Examples %}
|
||||||
|
|
||||||
@ -43,26 +42,26 @@ This example indicates true if a sensor is above a given threshold. Assuming a s
|
|||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
sensor:
|
sensor:
|
||||||
platform: template
|
- platform: template
|
||||||
sensors:
|
sensors:
|
||||||
furnace_on:
|
furnace_on:
|
||||||
value_template: {% raw %}{{ states.sensor.furnace.state > 2.5 }}{% endraw %}
|
value_template: {% raw %}{{ states.sensor.furnace.state > 2.5 }}{% endraw %}
|
||||||
friendly_name: 'Furnace Running
|
friendly_name: 'Furnace Running
|
||||||
sensor_class: heat
|
sensor_class: heat
|
||||||
```
|
```
|
||||||
|
|
||||||
### {% linkable_title Switch as sensor %}
|
### {% linkable_title Switch as sensor %}
|
||||||
|
|
||||||
Some movement sensors and door/window sensors will apear as a switch. By using a template binary sensor, the switch can be displayed as a binary sensors. The original switch can then be hidden by [customizing.](/getting-started/customizing-devices/)
|
Some movement sensors and door/window sensors will appear as a switch. By using a template binary sensor, the switch can be displayed as a binary sensors. The original switch can then be hidden by [customizing.](/getting-started/customizing-devices/)
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
binary_sensor:
|
binary_sensor:
|
||||||
platform: template
|
- platform: template
|
||||||
sensors:
|
sensors:
|
||||||
movement:
|
movement:
|
||||||
value_template: {% raw %}"{{ states.switch.movement.state == 'on' }}"{% endraw %}
|
value_template: {% raw %}"{{ states.switch.movement.state == 'on' }}"{% endraw %}
|
||||||
sensor_class: motion
|
sensor_class: motion
|
||||||
door:
|
door:
|
||||||
value_template: {% raw %}"{{ states.switch.door.state == 'on' }}"{% endraw %}
|
value_template: {% raw %}"{{ states.switch.door.state == 'on' }}"{% endraw %}
|
||||||
sensor_class: opening
|
sensor_class: opening
|
||||||
```
|
```
|
||||||
|
@ -18,14 +18,14 @@ To enable Template sensors in your installation, add the following to your `conf
|
|||||||
```yaml
|
```yaml
|
||||||
# Example configuration.yaml entry
|
# Example configuration.yaml entry
|
||||||
sensor:
|
sensor:
|
||||||
platform: template
|
- platform: template
|
||||||
sensors:
|
sensors:
|
||||||
solar_angle:
|
solar_angle:
|
||||||
value_template: {% raw %}'{{ states.sun.sun.attributes.elevation }}'{% endraw %}
|
value_template: {% raw %}'{{ states.sun.sun.attributes.elevation }}'{% endraw %}
|
||||||
friendly_name: 'Sun angle'
|
friendly_name: 'Sun angle'
|
||||||
unit_of_measurement: 'degrees'
|
unit_of_measurement: 'degrees'
|
||||||
sunrise:
|
sunrise:
|
||||||
value_template: {% raw %}'{{ states.sun.sun.attributes.next_rising }}'{% endraw %}
|
value_template: {% raw %}'{{ states.sun.sun.attributes.next_rising }}'{% endraw %}
|
||||||
```
|
```
|
||||||
|
|
||||||
Configuration variables:
|
Configuration variables:
|
||||||
@ -34,8 +34,7 @@ Configuration variables:
|
|||||||
- **friendly_name** (*Optional*): Name to use in the Frontend.
|
- **friendly_name** (*Optional*): Name to use in the Frontend.
|
||||||
- **unit_of_measurement** (*Optional*): Defines the units of measurement of the sensor, if any.
|
- **unit_of_measurement** (*Optional*): Defines the units of measurement of the sensor, if any.
|
||||||
- **value_template** (*Optional*): Defines a [template](/topics/templating/) to extract a value from the payload.
|
- **value_template** (*Optional*): Defines a [template](/topics/templating/) to extract a value from the payload.
|
||||||
- **warnings** (*Optional*): Turn off warnings (useful if the sensor is loaded before devices it depends on).
|
- **entity_id** (*Optional*): Add a list of entity IDs so the sensor only reacts to state changes of these entities. This will reduce the number of times the sensor will try to update it's state.
|
||||||
- **entity_id** (*Optional*): Add a list of entity_ids so the sensor only reacts to state changes of these entities. This will reduce the number of times the sensor will try to update it's state.
|
|
||||||
|
|
||||||
|
|
||||||
## {% linkable_title Examples %}
|
## {% linkable_title Examples %}
|
||||||
@ -48,12 +47,12 @@ This example shows the sun angle in the frontend.
|
|||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
sensor:
|
sensor:
|
||||||
platform: template
|
- platform: template
|
||||||
sensors:
|
sensors:
|
||||||
solar_angle:
|
solar_angle:
|
||||||
value_template: {% raw %}'{{ "%+.1f"|format(states.sun.sun.attributes.elevation) }}'{% endraw %}
|
value_template: {% raw %}'{{ "%+.1f"|format(states.sun.sun.attributes.elevation) }}'{% endraw %}
|
||||||
friendly_name: 'Sun Angle'
|
friendly_name: 'Sun Angle'
|
||||||
unit_of_measurement: '°'
|
unit_of_measurement: '°'
|
||||||
```
|
```
|
||||||
|
|
||||||
### {% linkable_title Renaming sensor output %}
|
### {% linkable_title Renaming sensor output %}
|
||||||
@ -62,22 +61,22 @@ If you don't like the wording of a sensor output then the template sensor can he
|
|||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
sensor:
|
sensor:
|
||||||
platform: template
|
- platform: template
|
||||||
sensors:
|
sensors:
|
||||||
sun_state:
|
sun_state:
|
||||||
value_template: {% raw %}'{% if is_state("sun.sun", "above_horizon") %}up{% else %}down{% endif %}'{% endraw %}
|
value_template: {% raw %}'{% if is_state("sun.sun", "above_horizon") %}up{% else %}down{% endif %}'{% endraw %}
|
||||||
friendly_name: 'Sun state'
|
friendly_name: 'Sun state'
|
||||||
```
|
```
|
||||||
|
|
||||||
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.
|
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.
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
sensor:
|
sensor:
|
||||||
platform: template
|
- platform: template
|
||||||
sensors:
|
sensors:
|
||||||
glances:
|
glances:
|
||||||
value_template: {% raw %}'{% if is_state("sensor.process_glances", "off") %}not running{% else %}running{% endif %}'{% endraw %}
|
value_template: {% raw %}'{% if is_state("sensor.process_glances", "off") %}not running{% else %}running{% endif %}'{% endraw %}
|
||||||
friendly_name: 'Glances'
|
friendly_name: 'Glances'
|
||||||
```
|
```
|
||||||
|
|
||||||
By comparing the details published on the [template](/topics/templating/) page the same can be archived with a different approach:
|
By comparing the details published on the [template](/topics/templating/) page the same can be archived with a different approach:
|
||||||
@ -88,31 +87,34 @@ value_template: {% raw %}"{%if states.sensor.ENTITY_ID.state == 'on' %}running{%
|
|||||||
|
|
||||||
The [Binary template sensor](/components/binary_sensor.template/) is the one in similar cases if you prefer to see an icon instead of text.
|
The [Binary template sensor](/components/binary_sensor.template/) is the one in similar cases if you prefer to see an icon instead of text.
|
||||||
|
|
||||||
### {% linkable_title Multiline example with an if test (and warnings disabled) %}
|
### {% linkable_title Multiline example with an if test %}
|
||||||
|
|
||||||
This example shows a multiple line template with and if test. It looks at a sensing switch and shows on/off in the frontend. It disables warnings to avoid log messages where the switch it depends on isn't loaded yet.
|
This example shows a multiple line template with and if test. It looks at a sensing switch and shows on/off in the frontend.
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
sensor:
|
sensor:
|
||||||
platform: template
|
- platform: template
|
||||||
sensors:
|
sensors:
|
||||||
kettle:
|
kettle:
|
||||||
friendly_name: 'Kettle'
|
friendly_name: 'Kettle'
|
||||||
{% raw %}value_template: >-
|
{% raw %}value_template: >-
|
||||||
{%- if is_state("switch.kettle", "off") %}
|
{%- if is_state("switch.kettle", "off") %}
|
||||||
off
|
off
|
||||||
{% elif states.switch.kettle.attributes.kwh < 1000 %}
|
{% elif states.switch.kettle.attributes.kwh < 1000 %}
|
||||||
standby
|
standby
|
||||||
{% elif is_state("switch.kettle", "on") %}
|
{% elif is_state("switch.kettle", "on") %}
|
||||||
on
|
on
|
||||||
{% else %}
|
{% else %}
|
||||||
failed
|
failed
|
||||||
{%- endif %}{% endraw %}
|
{%- endif %}{% endraw %}
|
||||||
|
|
||||||
warnings: Off
|
next_sensor:
|
||||||
|
[...]
|
||||||
```
|
```
|
||||||
|
|
||||||
(please note the blank line to close the multi-line template)
|
<p class='note'>
|
||||||
|
Please note the blank line to close the multi-line template.
|
||||||
|
</p>
|
||||||
|
|
||||||
### {% linkable_title Change the unit of measurment %}
|
### {% linkable_title Change the unit of measurment %}
|
||||||
|
|
||||||
@ -120,15 +122,15 @@ With a template sensor it's easy to convert given values into others if the unit
|
|||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
sensor:
|
sensor:
|
||||||
platform: template
|
- platform: template
|
||||||
sensors:
|
sensors:
|
||||||
transmission_down_speed_kbps:
|
transmission_down_speed_kbps:
|
||||||
value_template: {% raw %}'{{ states.sensor.transmission_down_speed.state | multiply(1024) }}'{% endraw %}
|
value_template: {% raw %}'{{ states.sensor.transmission_down_speed.state | multiply(1024) }}'{% endraw %}
|
||||||
friendly_name: 'Transmission Down Speed'
|
friendly_name: 'Transmission Down Speed'
|
||||||
unit_of_measurement: 'kB/s'
|
unit_of_measurement: 'kB/s'
|
||||||
transmission_up_speed_kbps:
|
transmission_up_speed_kbps:
|
||||||
value_template: {% raw %}'{{ states.sensor.transmission_up_speed.state | multiply(1024) }}'{% endraw %}
|
value_template: {% raw %}'{{ states.sensor.transmission_up_speed.state | multiply(1024) }}'{% endraw %}
|
||||||
friendly_name: 'Transmission Up Speed'
|
friendly_name: 'Transmission Up Speed'
|
||||||
unit_of_measurement: 'kB/s'
|
unit_of_measurement: 'kB/s'
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -22,17 +22,17 @@ To enable Template switches in your installation, add the following to your `con
|
|||||||
```yaml
|
```yaml
|
||||||
# Example configuration.yaml entry
|
# Example configuration.yaml entry
|
||||||
switch:
|
switch:
|
||||||
platform: template
|
- platform: template
|
||||||
switches:
|
switches:
|
||||||
skylight:
|
skylight:
|
||||||
friendly_name: 'Skylight'
|
friendly_name: 'Skylight'
|
||||||
value_template: {% raw %}'{{ is_state('sensor.skylight', 'on') }}'{% endraw %}
|
value_template: {% raw %}'{{ is_state('sensor.skylight', 'on') }}'{% endraw %}
|
||||||
turn_on:
|
turn_on:
|
||||||
service: switch.turn_on
|
service: switch.turn_on
|
||||||
entity_id: switch.skylight_open
|
entity_id: switch.skylight_open
|
||||||
turn_off:
|
turn_off:
|
||||||
service: switch.turn_on
|
service: switch.turn_on
|
||||||
entity_id: switch.skylight_close
|
entity_id: switch.skylight_close
|
||||||
```
|
```
|
||||||
|
|
||||||
Configuration variables:
|
Configuration variables:
|
||||||
@ -42,13 +42,13 @@ Configuration variables:
|
|||||||
- **value_template** (*Required*): Defines a [template](/topics/templating/) to set the state of the switch.
|
- **value_template** (*Required*): Defines a [template](/topics/templating/) to set the state of the switch.
|
||||||
- **turn_on** (*Required*): Defines an [action](/getting-started/automation/) to run when the switch is turned on.
|
- **turn_on** (*Required*): Defines an [action](/getting-started/automation/) to run when the switch is turned on.
|
||||||
- **turn_off** (*Required*): Defines an [action](/getting-started/automation/) to run when the switch is turned off.
|
- **turn_off** (*Required*): Defines an [action](/getting-started/automation/) to run when the switch is turned off.
|
||||||
- **entity_id** (*Optional*): Add a list of entity_ids so the sensor only reacts to state changes of these entities. This will reduce the number of times the sensor will try to update it's state.
|
- **entity_id** (*Optional*): Add a list of entity IDs so the switch only reacts to state changes of these entities. This will reduce the number of times the switch will try to update it's state.
|
||||||
|
|
||||||
|
|
||||||
## {% linkable_title Considerations %}
|
## {% linkable_title Considerations %}
|
||||||
|
|
||||||
If you are using the state of a platform that takes extra time to load, the template switch may get an 'unknown' state during startup. This results in error messages in your log file until that platform has completed loading. If you use is_state() function in your template, you can avoid this situation. For example, you would replace {% raw %}'{{ states.switch.source.state }}'{% endraw %} with this equivalent that returns true/false and never gives an unknown result:
|
If you are using the state of a platform that takes extra time to load, the template switch may get an 'unknown' state during startup. This results in error messages in your log file until that platform has completed loading. If you use is_state() function in your template, you can avoid this situation. For example, you would replace {% raw %}'{{ states.switch.source.state }}'{% endraw %} with this equivalent that returns true/false and never gives an unknown result:
|
||||||
{% raw %}'{{ is_state('switch.source', 'on') }}'{% stendraw %}
|
{% raw %}'{{ is_state('switch.source', 'on') }}'{% endraw %}
|
||||||
|
|
||||||
## {% linkable_title Examples %}
|
## {% linkable_title Examples %}
|
||||||
|
|
||||||
@ -60,16 +60,16 @@ This example shows a switch that copies another switch.
|
|||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
switch:
|
switch:
|
||||||
platform: template
|
- platform: template
|
||||||
switches:
|
switches:
|
||||||
copy:
|
copy:
|
||||||
value_template: {% raw %}'{{ is_state('switch.source', 'on') }}'{% endraw %}
|
value_template: {% raw %}'{{ is_state('switch.source', 'on') }}'{% endraw %}
|
||||||
turn_on:
|
turn_on:
|
||||||
service: switch.turn_on
|
service: switch.turn_on
|
||||||
entity_id: switch.source
|
entity_id: switch.source
|
||||||
turn_off:
|
turn_off:
|
||||||
service: switch.turn_off
|
service: switch.turn_off
|
||||||
entity_id: switch.source
|
entity_id: switch.source
|
||||||
````
|
````
|
||||||
|
|
||||||
### {% linkable_title Toggle switch %}
|
### {% linkable_title Toggle switch %}
|
||||||
@ -78,17 +78,17 @@ This example shows a switch that takes its state from a sensor, and toggles a sw
|
|||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
switch:
|
switch:
|
||||||
platform: template
|
- platform: template
|
||||||
switches:
|
switches:
|
||||||
blind:
|
blind:
|
||||||
friendly_name: 'Blind'
|
friendly_name: 'Blind'
|
||||||
value_template: {% raw %}'{{ state }}'{% endraw %}
|
value_template: {% raw %}'{{ state }}'{% endraw %}
|
||||||
turn_on:
|
turn_on:
|
||||||
service: switch.toggle
|
service: switch.toggle
|
||||||
entity_id: switch.blind_toggle
|
entity_id: switch.blind_toggle
|
||||||
turn_off:
|
turn_off:
|
||||||
service: switch.toggle
|
service: switch.toggle
|
||||||
entity_id: switch.blind_toggle
|
entity_id: switch.blind_toggle
|
||||||
```
|
```
|
||||||
|
|
||||||
### {% linkable_title Sensor and two switches %}
|
### {% linkable_title Sensor and two switches %}
|
||||||
@ -97,15 +97,16 @@ This example shows a switch that takes its state from a sensor, and uses two mom
|
|||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
switch:
|
switch:
|
||||||
platform: template
|
- platform: template
|
||||||
switches:
|
switches:
|
||||||
skylight:
|
skylight:
|
||||||
friendly_name: 'Skylight'
|
friendly_name: 'Skylight'
|
||||||
value_template: {% raw %}'{{ is_state('sensor.skylight.state', 'on') }}'{% endraw %}
|
value_template: {% raw %}'{{ is_state('sensor.skylight.state', 'on') }}'{% endraw %}
|
||||||
turn_on:
|
turn_on:
|
||||||
service: switch.turn_on
|
service: switch.turn_on
|
||||||
entity_id: switch.skylight_open
|
entity_id: switch.skylight_open
|
||||||
turn_off:
|
turn_off:
|
||||||
service: switch.turn_on
|
service: switch.turn_on
|
||||||
entity_id: switch.skylight_close
|
entity_id: switch.skylight_close
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user