Custom Binary Sensor

This integration can be used to create custom binary sensors in ESPHome using the C++ (Arduino) API.

Please first read Custom Sensor Component guide, 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.

#include "esphome.h"

class MyCustomBinarySensor : public PollingComponent, public BinarySensor {
  // constructor
  MyCustomBinarySensor() : PollingComponent(15000) {}

  void setup() override {
    // This will be called by App.setup()
    pinMode(5, INPUT);
  void update() override {
    // This will be called every "update_interval" milliseconds.

    // Publish an OFF state
    bool state = digitalRead(5);

(Store this file in your configuration directory, for example my_binary_sensor.h)

And in YAML:

# Example configuration entry
    - my_binary_sensor.h

  - platform: custom
    lambda: |-
      auto my_custom_sensor = new MyCustomBinarySensor();
      return {my_custom_sensor};

      name: "My Custom Binary Sensor"

Configuration variables:

  • lambda (Required, 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.

See BinarySensor

See Also