Binary Sensor

In esphomelib, every component that exposes a binary state, is a BinarySensor.

To create your own binary sensor, simply subclass BinarySensor and call BinarySensor::publish_state() to tell the frontend that you have a new state. Inversion is automatically done for you when publishing state and can be changed by the user with BinarySensor::set_inverted().

Example Usage

// Basic
// GPIO Binary Sensor
App.make_gpio_binary_sensor("Window Open", 36);

See Application::register_binary_sensor() and Application::make_gpio_binary_sensor().

API Reference


class BinarySensor : public Nameable

Base class for all binary_sensor-type classes.

This class includes a callback that components such as MQTT can subscribe to for state changes. The sub classes should notify the front-end of new states via the publish_state() method which handles inverted inputs for you.

Subclassed by binary_sensor::ESP32TouchBinarySensor, binary_sensor::GPIOBinarySensorComponent, binary_sensor::PN532BinarySensor, binary_sensor::RDM6300BinarySensor, binary_sensor::StatusBinarySensor, binary_sensor::TemplateBinarySensor, display::NextionTouchComponent, ESP32BLEPresenceDevice, remote::RemoteReceiver

Public Functions

BinarySensor(const std::string &name)

Construct a binary sensor with the specified name.

  • name: Name of this binary sensor.

void add_on_state_callback(binary_callback_t &&callback)

Set callback for state changes.

  • callback: The void(bool) callback.

void publish_state(bool state)

Publish a new state.

Inverted input is handled by this method and sub-classes don’t need to worry about inverting themselves.

  • state: The new state.

bool get_value() const

Get the current boolean value of this binary sensor.

void set_device_class(const std::string &device_class)

Manually set the Home Assistant device class (see esphomelib::binary_sensor::device_class)

std::string get_device_class()

Get the device class for this binary sensor, using the manual override if specified.

PressTrigger *make_press_trigger()
ReleaseTrigger *make_release_trigger()
ClickTrigger *make_click_trigger(uint32_t min_length, uint32_t max_length)
DoubleClickTrigger *make_double_click_trigger(uint32_t min_length, uint32_t max_length)
void add_filter(Filter *filter)
void add_filters(std::vector<Filter *> filters)
void send_value_(bool state)

Public Members

bool value = {false}

Protected Functions

std::string device_class()

Get the default device class for this sensor, or empty string for no default.

Protected Attributes

bool last_raw_
bool is_first_raw_ = {true}
bool last_value_
bool is_first_value_ = {true}
CallbackManager<void(bool)> state_callback_ = {}
optional<std::string> device_class_ = {}

Stores the override of the device class.

Filter *filter_list_ = {nullptr}


class MQTTBinarySensorComponent : public mqtt::MQTTComponent

Simple MQTT front-end component for a binary_sensor.

After construction of this class, it should be connected to the BinarySensor by setting the callback returned by create_on_new_state_callback() in BinarySensor::on_new_state().

Public Functions

MQTTBinarySensorComponent(BinarySensor *binary_sensor)

Construct a MQTTBinarySensorComponent.

  • binary_sensor: The binary sensor.

void setup()

Send discovery.

void send_discovery(JsonBuffer &buffer, JsonObject &obj, mqtt::SendDiscoveryConfig &config)

Send Home Assistant discovery info.

const std::string &get_payload_on() const

Get the payload this binary sensor uses for an ON value.

void set_payload_on(std::string payload_on)

Set the custom payload this binary sensor uses for an ON value.

const std::string &get_payload_off() const

Get the payload this binary sensor uses for an OFF value.

void set_payload_off(std::string payload_off)

Set the custom payload this binary sensor uses for an OFF value.

void send_initial_state()
void publish_state(bool state)
bool is_internal()

Protected Functions

std::string friendly_name() const

Return the friendly name of this binary sensor.

std::string component_type() const

“binary_sensor” component type.

Protected Attributes

BinarySensor *binary_sensor_
std::string payload_on_ = {"ON"}
std::string payload_off_ = {"OFF"}