Template Binary Sensor¶
The template
binary sensor platform allows you to define any lambda template
and construct a binary sensor out if it. The lambda will run continuously; it isn’t possible to specify
an interval at which the lambda runs.
For example, below configuration would turn the state of an ultrasonic sensor into a binary sensor.
# Example configuration entry
binary_sensor:
- platform: template
name: "Garage Door Open"
lambda: |-
if (id(ultrasonic_sensor1).state > 30) {
// Garage Door is open.
return true;
} else {
// Garage Door is closed.
return false;
}
Possible return values of the lambda:
return true;
if the binary sensor should be ON.
return false;
if the binary sensor should be OFF.
return {};
if the state is not known (use last known state)
Configuration variables:¶
lambda (Optional, lambda): Lambda to be evaluated repeatedly to get the current state of the binary sensor.
All other options from Binary Sensor.
binary_sensor.template.publish
Action¶
You can also publish a state to a template binary sensor from elsewhere in your YAML file
with the binary_sensor.template.publish
action.
# Example configuration entry
binary_sensor:
- platform: template
name: "Garage Door Open"
id: template_bin
# in some trigger
on_...:
- binary_sensor.template.publish:
id: template_bin
state: ON
# Templated
- binary_sensor.template.publish:
id: template_bin
state: !lambda 'return id(some_sensor).state > 30;'
Configuration options:
id (Required, ID): The ID of the template binary sensor.
state (Required, boolean, templatable): The state to publish.
Note
This action can also be written in lambdas:
id(template_bin).publish_state(true);