esphome-docs/components/binary_sensor/template.rst

96 lines
2.7 KiB
ReStructuredText
Raw Normal View History

2018-06-01 18:10:00 +02:00
Template Binary Sensor
======================
2018-11-14 22:12:27 +01:00
.. seo::
:description: Instructions for setting up template binary sensors.
:image: description.svg
2018-11-14 22:12:27 +01:00
2018-06-01 18:10:00 +02:00
The ``template`` binary sensor platform allows you to define any :ref:`lambda template <config-lambda>`
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.
2018-06-01 18:10:00 +02:00
For example, below configuration would turn the state of an ultrasonic sensor into
a binary sensor.
.. code-block:: yaml
2018-06-01 18:10:00 +02:00
# Example configuration entry
binary_sensor:
- platform: template
name: "Garage Door Open"
2019-01-06 18:56:14 +01:00
lambda: |-
2019-02-16 23:25:23 +01:00
if (id(ultrasonic_sensor1).state > 30) {
2018-06-01 18:10:00 +02:00
// 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.
2019-02-16 23:25:23 +01:00
- ``return {};`` if the state is not known (use last known state)
2018-06-01 18:10:00 +02:00
Configuration variables:
2018-08-24 22:44:01 +02:00
------------------------
2018-06-01 18:10:00 +02:00
- **name** (**Required**, string): The name of the binary sensor.
2019-02-16 23:25:23 +01:00
- **lambda** (*Optional*, :ref:`lambda <config-lambda>`):
2018-06-01 18:10:00 +02:00
Lambda to be evaluated repeatedly to get the current state of the binary sensor.
- **id** (*Optional*,
:ref:`config-id`): Manually specify
the ID used for code generation.
2019-02-17 12:28:17 +01:00
- All other options from :ref:`Binary Sensor <config-binary_sensor>`.
2018-06-01 18:10:00 +02:00
2019-02-16 23:25:23 +01:00
.. _binary_sensor-template-publish_action:
``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.
.. code-block:: yaml
# 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**, :ref:`config-id`): The ID of the template binary sensor.
- **state** (**Required**, boolean, :ref:`templatable <config-templatable>`):
The state to publish.
.. note::
This action can also be written in lambdas:
.. code-block:: cpp
id(template_bin).publish_state(true);
2018-06-01 18:10:00 +02:00
See Also
2018-08-24 22:44:01 +02:00
--------
2018-06-01 18:10:00 +02:00
- :doc:`/components/binary_sensor/index`
- :doc:`/components/sensor/template`
2018-06-01 18:10:00 +02:00
- :ref:`automation`
2019-05-12 22:44:59 +02:00
- :apiref:`template/binary_sensor/template_binary_sensor.h`
- :ghedit:`Edit`