esphome-docs/esphomeyaml/components/binary_sensor/template.rst

63 lines
2.1 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-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.
For example, below configuration would turn the state of an ultrasonic sensor into
a binary sensor.
.. code:: yaml
# Example configuration entry
binary_sensor:
- platform: template
name: "Garage Door Open"
lambda: >-
if (isnan(id(ultrasonic_sensor1).state)) {
2018-06-01 18:10:00 +02:00
// isnan checks if the ultrasonic sensor echo
// has timed out, resulting in a NaN (not a number) state
// in that case, return {} to indicate that we don't know.
return {};
} else 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.
- ``return {};`` if the last state should be repeated.
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.
- **lambda** (**Required**, :ref:`lambda <config-lambda>`):
Lambda to be evaluated repeatedly to get the current state of the binary sensor.
Only state *changes* will be published to MQTT.
- **id** (*Optional*,
:ref:`config-id`): Manually specify
the ID used for code generation.
- All other options from :ref:`Binary Sensor <config-binary_sensor>`
and :ref:`MQTT Component <config-mqtt-component>`.
See Also
2018-08-24 22:44:01 +02:00
--------
2018-06-01 18:10:00 +02:00
- :doc:`/esphomeyaml/components/binary_sensor/index`
- :doc:`/esphomeyaml/components/sensor/template`
- :ref:`automation`
- :doc:`API Reference </api/binary_sensor/template>`
2018-06-04 08:17:22 +02:00
- `Edit this page on GitHub <https://github.com/OttoWinter/esphomedocs/blob/current/esphomeyaml/components/binary_sensor/template.rst>`__
2018-10-12 16:33:22 +02:00
.. disqus::