2018-11-19 22:28:21 +01:00
|
|
|
Custom Binary Sensor
|
|
|
|
====================
|
|
|
|
|
2019-02-16 23:25:23 +01:00
|
|
|
This integration can be used to create custom binary sensors in ESPHome
|
2018-11-19 22:28:21 +01:00
|
|
|
using the C++ (Arduino) API.
|
|
|
|
|
2019-02-07 13:54:45 +01:00
|
|
|
Please first read :doc:`/components/sensor/custom` guide,
|
2018-11-27 17:05:23 +01:00
|
|
|
the same principles apply here and binary sensors are very similar
|
|
|
|
to sensors internally.
|
|
|
|
|
|
|
|
The example below is an example of a custom binary sensor; this custom sensor is essentially the
|
|
|
|
same as the gpio binary sensor.
|
2018-11-19 22:28:21 +01:00
|
|
|
|
|
|
|
.. code-block:: cpp
|
|
|
|
|
2019-02-16 23:25:23 +01:00
|
|
|
#include "esphome.h"
|
|
|
|
using namespace esphome;
|
2018-11-19 22:28:21 +01:00
|
|
|
|
|
|
|
class MyCustomBinarySensor : public PollingComponent, public binary_sensor::BinarySensor {
|
|
|
|
public:
|
|
|
|
// constructor
|
|
|
|
MyCustomBinarySensor() : PollingComponent(15000) {}
|
|
|
|
|
|
|
|
void setup() override {
|
|
|
|
// This will be called by App.setup()
|
2018-11-27 17:05:23 +01:00
|
|
|
pinMode(5, INPUT);
|
2018-11-19 22:28:21 +01:00
|
|
|
}
|
|
|
|
void update() override {
|
|
|
|
// This will be called every "update_interval" milliseconds.
|
|
|
|
|
|
|
|
// Publish an OFF state
|
2019-02-18 18:49:26 +01:00
|
|
|
bool state = digitalRead(5);
|
2018-11-27 17:05:23 +01:00
|
|
|
publish_state(state);
|
2018-11-19 22:28:21 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-26 16:50:50 +01:00
|
|
|
(Store this file in your configuration directory, for example ``my_binary_sensor.h``)
|
2018-11-19 22:28:21 +01:00
|
|
|
|
|
|
|
And in YAML:
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
# Example configuration entry
|
2019-02-16 23:25:23 +01:00
|
|
|
esphome:
|
2018-11-26 16:50:50 +01:00
|
|
|
includes:
|
|
|
|
- my_binary_sensor.h
|
|
|
|
|
2018-11-19 22:28:21 +01:00
|
|
|
binary_sensor:
|
2019-02-18 18:49:26 +01:00
|
|
|
- platform: custom
|
|
|
|
lambda: |-
|
|
|
|
auto my_custom_sensor = new MyCustomBinarySensor();
|
|
|
|
App.register_component(my_custom_sensor);
|
|
|
|
return {my_custom_sensor};
|
|
|
|
|
|
|
|
binary_sensors:
|
|
|
|
name: "My Custom Binary Sensor"
|
2018-11-19 22:28:21 +01:00
|
|
|
|
|
|
|
Configuration variables:
|
|
|
|
|
|
|
|
- **lambda** (**Required**, :ref:`lambda <config-lambda>`): The lambda to run for instantiating the
|
|
|
|
binary sensor(s).
|
|
|
|
- **binary_sensors** (**Required**, list): A list of binary sensors to initialize. The length here
|
|
|
|
must equal the number of items in the ``return`` statement of the ``lambda``.
|
|
|
|
|
2019-02-17 12:28:17 +01:00
|
|
|
- All options from :ref:`Binary Sensor <config-binary_sensor>`.
|
2018-11-19 22:28:21 +01:00
|
|
|
|
2018-11-27 17:05:23 +01:00
|
|
|
See :cpp:class:`binary_sensor::BinarySensor`
|
|
|
|
|
2018-11-19 22:28:21 +01:00
|
|
|
See Also
|
|
|
|
--------
|
|
|
|
|
2019-02-07 13:54:45 +01:00
|
|
|
- :ghedit:`Edit`
|
2019-03-06 11:02:03 +01:00
|
|
|
|
|
|
|
.. disqus::
|