EnigmaIOT  0.9.8
Secure sensor and gateway platform based on ESP8266 and ESP32
Public Member Functions | Static Public Member Functions | List of all members
HABinarySensor Class Reference

#include <haBinarySensor.h>

Inheritance diagram for HABinarySensor:
HAEntity

Public Member Functions

 HABinarySensor ()
 Binary sensor constructor. More...
 
void setDeviceClass (haBinarySensorClass_t devClass)
 Define binary sensor class as haBinarySensorClass_t More...
 
void setPayloadOn (const char *payload)
 Defines the string that represents the on state. It will be compared to the message in the state_topic (see value_template for details) https://www.home-assistant.io/integrations/binary_sensor.mqtt/#payload_on. More...
 
void setPayloadOff (const char *payload)
 Defines the string that represents the off state. It will be compared to the message in the state_topic (see value_template for details) https://www.home-assistant.io/integrations/binary_sensor.mqtt/#payload_off. More...
 
void setPayloadOn (int payload)
 Defines a number that represents the on state. It will be compared to the message in the state_topic (see value_template for details) https://www.home-assistant.io/integrations/binary_sensor.mqtt/#payload_on. More...
 
void setPayloadOff (int payload)
 Defines a number that represents the off state. It will be compared to the message in the state_topic (see value_template for details) https://www.home-assistant.io/integrations/binary_sensor.mqtt/#payload_off. More...
 
void setValueField (const char *payload)
 Defines a template that returns a string to be compared to payload_on/payload_off or an empty string, in which case the MQTT message will be removed. Available variables: entity_id. Remove this option when ‘payload_on’ and ‘payload_off’ are sufficient to match your payloads (i.e no pre-processing of original message is required) When setting this, setValueTemplate () should not be called https://www.home-assistant.io/integrations/binary_sensor.mqtt/#value_template. More...
 
void setValueTemplate (const char *payload)
 Defines a template that defines binary sensor value. When setting this you should not call setValueField () https://www.home-assistant.io/integrations/binary_sensor.mqtt/#value_template. More...
 
void setOffDelay (uint payload)
 For sensors that only send on state updates (like PIRs), this sets a delay in seconds after which the sensor’s state will be updated back to off. https://www.home-assistant.io/integrations/binary_sensor.mqtt/#off_delay. More...
 
void addExpiration (uint seconds)
 Defines the number of seconds after the sensor’s state expires, if it’s not updated. After expiry, the sensor’s state becomes unavailable. https://www.home-assistant.io/integrations/binary_sensor.mqtt/#expire_after. More...
 
- Public Member Functions inherited from HAEntity
size_t getAnounceMessage (int bufferlen, uint8_t *buffer)
 Gets entity anounce message to be sent over EnigmaIOT message. More...
 
void setNameSufix (const char *payload)
 Sets name suffix. Used for multi entity nodes. More...
 
void allowSendAttributes ()
 Enables registering entity attributes as a json object. More...
 
size_t measureMessage ()
 Gets needed buffer size for discovery message. More...
 

Static Public Member Functions

static size_t getDiscoveryJson (char *buffer, size_t buflen, const char *nodeName, const char *networkName, DynamicJsonDocument *inputJSON)
 Allows Gateway to get Home Assistant discovery message using Binary Sensor template. More...
 
static String deviceClassStr (haBinarySensorClass_t sensorClass)
 Gets binary sensor class name from haBinarySensorClass_t https://www.home-assistant.io/integrations/binary_sensor/#device-class. More...
 
- Static Public Member Functions inherited from HAEntity
static String deviceTypeStr (haDeviceType_t entityType)
 Gets entity type string from haDeviceType_t value https://www.home-assistant.io/docs/mqtt/discovery/. More...
 
static String getDiscoveryTopic (const char *hassPrefix, const char *nodeName, haDeviceType_t entityType, const char *nameSuffix=NULL)
 Allows Gateway to get discovery message MQTT topic. More...
 

Additional Inherited Members

- Protected Member Functions inherited from HAEntity
 HAEntity ()
 Default constructor. Needed for inheritance. More...
 
- Protected Attributes inherited from HAEntity
size_t capacity
 JSON object memory reservation length. More...
 
haDeviceType_t deviceType = UNDEFINED
 HomeAssistant entity device type. More...
 
DynamicJsonDocument * entityConfig
 JSON object to be sent to gateway. More...
 

Detailed Description

The mqtt binary sensor platform uses an MQTT message received to set the binary sensor’s state to on or off. The state will be updated only after a new message is published on state_topic matching payload_on or payload_off. If these messages are published with the retain flag set, the binary sensor will receive an instant state update after subscription and Home Assistant will display the correct state on startup. Otherwise, the initial state displayed in Home Assistant will be unknown.

Stateless devices such as buttons, remote controls etc are better represented by MQTT device triggers than by binary sensors.

https://www.home-assistant.io/integrations/binary_sensor.mqtt/

An example of binary sensor discovery message may be like this:

Topic: homeassistant/binary_sensor/thermostat/config

Payload:

{ "name":"thermostat", // string (optional, default: MQTT Binary Sensor) The name of the binary sensor. "unique_id":"thermostat", // string (optional) An ID that uniquely identifies this sensor. // If two sensors have the same unique ID, Home Assistant will raise an exception. "state_topic":"EnigmaIOT/thermostat/data", // string REQUIRED. The MQTT topic subscribed to receive sensor’s state. "payload_on":"ON", // string (optional, default: ON) The string that represents the on state. // It will be compared to the message in the state_topic (see value_template for details) "payload_off":"OFF", // string (optional, default: OFF) The string that represents the off state. // It will be compared to the message in the state_topic (see value_template for details) "value_template":"{{value_json.activation}}", // Defines a template that returns a string to be compared to payload_on/payload_off // or an empty string, in which case the MQTT message will be removed. // Available variables: entity_id. // Remove this option when ‘payload_on’ and ‘payload_off’ are sufficient to match // your payloads (i.e no pre-processing of original message is required). "expire_after":30, // Defines the number of seconds after the sensor’s state expires, if it’s not updated. // After expiry, the sensor’s state becomes unavailable. "device_class":"heat" // Sets the class of the device, changing the device state and icon that is displayed on the frontend. }

Template message for binary sensor is this

Topic: homeassistant/binary_sensor/<node_name>/config

Payload { "name":<node_name>_<name_suffix>, "unique_id":<node_name>_<name_suffix>, "device_class":<device_class>, "state_topic":"<network_name>/<node_name>/data", "payload_on":<cmd_payload_on>, "payload_off":<cmd_payload_off>, "value_template":"{{value_json.<value_field>}}", "expire_after":<expire_time> }

Message template to gateway is like this

{ "pl_on":<cmd_payload_on>, "pl_off":<cmd_payload_off>, "val":<value_field>, "exp_aft":<expire_time>, "dev_cla":<device_class>, "nmsfx":<name_suffix> }

If any of the optional values (like "pl_on" or "pl_off") is not set its key will not be sent into message

Definition at line 85 of file haBinarySensor.h.

Constructor & Destructor Documentation

◆ HABinarySensor()

HABinarySensor::HABinarySensor ( )
inline

Binary sensor constructor.

Definition at line 90 of file haBinarySensor.h.

Member Function Documentation

◆ addExpiration()

void HABinarySensor::addExpiration ( uint  seconds)
inline

Defines the number of seconds after the sensor’s state expires, if it’s not updated. After expiry, the sensor’s state becomes unavailable. https://www.home-assistant.io/integrations/binary_sensor.mqtt/#expire_after.

Parameters
secondsExpiration time in seconds

Definition at line 178 of file haBinarySensor.h.

◆ deviceClassStr()

String HABinarySensor::deviceClassStr ( haBinarySensorClass_t  sensorClass)
static

Gets binary sensor class name from haBinarySensorClass_t https://www.home-assistant.io/integrations/binary_sensor/#device-class.

Parameters
sensorClassBinary sensor class code

Definition at line 142 of file haBinarySensor.cpp.

◆ getDiscoveryJson()

size_t HABinarySensor::getDiscoveryJson ( char *  buffer,
size_t  buflen,
const char *  nodeName,
const char *  networkName,
DynamicJsonDocument *  inputJSON 
)
static

Allows Gateway to get Home Assistant discovery message using Binary Sensor template.

Parameters
bufferBuffer to hold message string
buflenBuffer size
nodeNameOriginating node name
networkNameEnigmaIOT network name
inputJSONJSON object sent by node with needed data to fill template in
Returns
Discovery message payload

msgPack || !len

Definition at line 78 of file haBinarySensor.cpp.

◆ setDeviceClass()

void HABinarySensor::setDeviceClass ( haBinarySensorClass_t  devClass)

Define binary sensor class as haBinarySensorClass_t

Parameters
devClassDevice class

Definition at line 13 of file haBinarySensor.cpp.

◆ setOffDelay()

void HABinarySensor::setOffDelay ( uint  payload)

For sensors that only send on state updates (like PIRs), this sets a delay in seconds after which the sensor’s state will be updated back to off. https://www.home-assistant.io/integrations/binary_sensor.mqtt/#off_delay.

Parameters
payloadOff delay in seconds

Definition at line 52 of file haBinarySensor.cpp.

◆ setPayloadOff() [1/2]

void HABinarySensor::setPayloadOff ( const char *  payload)

Defines the string that represents the off state. It will be compared to the message in the state_topic (see value_template for details) https://www.home-assistant.io/integrations/binary_sensor.mqtt/#payload_off.

Parameters
payloadOFF state string

Definition at line 26 of file haBinarySensor.cpp.

◆ setPayloadOff() [2/2]

void HABinarySensor::setPayloadOff ( int  payload)

Defines a number that represents the off state. It will be compared to the message in the state_topic (see value_template for details) https://www.home-assistant.io/integrations/binary_sensor.mqtt/#payload_off.

Parameters
payloadOFF state value

Definition at line 36 of file haBinarySensor.cpp.

◆ setPayloadOn() [1/2]

void HABinarySensor::setPayloadOn ( const char *  payload)

Defines the string that represents the on state. It will be compared to the message in the state_topic (see value_template for details) https://www.home-assistant.io/integrations/binary_sensor.mqtt/#payload_on.

Parameters
payloadON state string

Definition at line 20 of file haBinarySensor.cpp.

◆ setPayloadOn() [2/2]

void HABinarySensor::setPayloadOn ( int  payload)

Defines a number that represents the on state. It will be compared to the message in the state_topic (see value_template for details) https://www.home-assistant.io/integrations/binary_sensor.mqtt/#payload_on.

Parameters
payloadON state value

Definition at line 32 of file haBinarySensor.cpp.

◆ setValueField()

void HABinarySensor::setValueField ( const char *  payload)

Defines a template that returns a string to be compared to payload_on/payload_off or an empty string, in which case the MQTT message will be removed. Available variables: entity_id. Remove this option when ‘payload_on’ and ‘payload_off’ are sufficient to match your payloads (i.e no pre-processing of original message is required) When setting this, setValueTemplate () should not be called https://www.home-assistant.io/integrations/binary_sensor.mqtt/#value_template.

Parameters
payloadON state value

Definition at line 40 of file haBinarySensor.cpp.

◆ setValueTemplate()

void HABinarySensor::setValueTemplate ( const char *  payload)

Defines a template that defines binary sensor value. When setting this you should not call setValueField () https://www.home-assistant.io/integrations/binary_sensor.mqtt/#value_template.

Parameters
payloadValue template definition

Definition at line 46 of file haBinarySensor.cpp.


The documentation for this class was generated from the following files: