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
HASensor Class Reference

#include <haSensor.h>

Inheritance diagram for HASensor:
HAEntity

Public Member Functions

 HASensor ()
 Sensor constructor. More...
 
void setDeviceClass (haSensorClass_t devClass)
 Define sensor class as haSensorClass_t https://www.home-assistant.io/integrations/sensor.mqtt/#device_class. More...
 
void setExpireTime (uint payload)
 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/sensor.mqtt/#expire_after. More...
 
void setUnitOfMeasurement (const char *payload)
 Set unit of measure https://www.home-assistant.io/integrations/sensor.mqtt/#unit_of_measurement. More...
 
void setValueField (const char *payload)
 Defines a json key that defines sensor value. When setting this, setValueTemplate () should not be called https://www.home-assistant.io/integrations/sensor.mqtt/#value_template. More...
 
void setValueTemplate (const char *payload)
 Defines a template that defines sensor value. When setting this you should not call setValueField () https://www.home-assistant.io/integrations/sensor.mqtt/#value_template. 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 Sensor template. More...
 
static String deviceClassStr (haSensorClass_t sensorClass)
 Gets sensor class name from haSensorClass_t https://www.home-assistant.io/integrations/sensor.mqtt/#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

This mqtt sensor platform uses the MQTT message payload as the sensor value. If messages in this state_topic are published with RETAIN flag, the sensor will receive an instant update with last known value. Otherwise, the initial state will be undefined.

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

An example of sensor discovery message may be like this:

Topic: homeassistant/sensor/mains_power/config

Payload:

{ "name":"mains_power", // string (optional, default: MQTT Sensor). The name of the MQTT sensor "unique_id":"mains_power", // string (optional) An ID that uniquely identifies this sensor. If two sensors have the same unique ID, // Home Assistant will raise an exception "device_class":"power", // device_class (optional, default: None). The type/class of the sensor to set the icon in the frontend // https://www.home-assistant.io/integrations/sensor/#device-class "expire_after":300, // integer (optional, default: 0) Defines the number of seconds after the sensor’s state expires, // if it’s not updated. After expiry, the sensor’s state becomes unavailable "json_attributes_template":"{{value_json | tojson}}", // template (optional) Defines a template to extract the JSON dictionary // from messages received on the json_attributes_topic "json_attributes_topic":"EnigmaIOT/mains/data", // string (optional) The MQTT topic subscribed to receive a JSON dictionary // payload and then set as sensor attributes. Implies force_update of the // current sensor state when a message is received on this topic "state_topic":"EnigmaIOT/mains/data", // string REQUIRED. The MQTT topic subscribed to receive sensor values "unit_of_measurement":"W"", // string (optional). Defines the units of measurement of the sensor, if any "value_template":"{{value_json.pow}}" // template (optional). Defines a template to extract the value }

Template message for sensor is this

Topic: homeassistant/sensor/<node_name>_<name_suffix>/config

Payload

{
"name":<node_name>_<name_suffix>,
"unique_id":<node_name>_<name_suffix>,
"device_class": <device_class>,
"expire_after":<expire_time>,
"json_attributes_template":"{{value_json | tojson}}",
"json_attributes_topic":"<network_name>/<node_name>/data",
"state_topic":"<network_name>/<node_name>/data",
"unit_of_measurement":<unit_of_measurement>,
"value_template":"{{value_json.<value_field>}}"
}

Message to gateway is like following

{
"dev_cla":<device_class>,
"exp_aft":<expire_time>,
"unit_of_meas":<unit_of_measurement>,
"val":<value_field>,
"nmsfx":<name_suffix>
}

Definition at line 81 of file haSensor.h.

Constructor & Destructor Documentation

◆ HASensor()

HASensor::HASensor ( )
inline

Sensor constructor.

Definition at line 87 of file haSensor.h.

Member Function Documentation

◆ deviceClassStr()

String HASensor::deviceClassStr ( haSensorClass_t  sensorClass)
static

Gets sensor class name from haSensorClass_t https://www.home-assistant.io/integrations/sensor.mqtt/#device_class.

Parameters
sensorClassBinary sensor class code

Definition at line 123 of file haSensor.cpp.

◆ getDiscoveryJson()

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

Allows Gateway to get Home Assistant discovery message using 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 65 of file haSensor.cpp.

◆ setDeviceClass()

void HASensor::setDeviceClass ( haSensorClass_t  devClass)

Define sensor class as haSensorClass_t https://www.home-assistant.io/integrations/sensor.mqtt/#device_class.

Parameters
devClassDevice class

Definition at line 13 of file haSensor.cpp.

◆ setExpireTime()

void HASensor::setExpireTime ( uint  payload)

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/sensor.mqtt/#expire_after.

Parameters
payloadExpiration value in seconds

Definition at line 20 of file haSensor.cpp.

◆ setUnitOfMeasurement()

void HASensor::setUnitOfMeasurement ( const char *  payload)

Set unit of measure https://www.home-assistant.io/integrations/sensor.mqtt/#unit_of_measurement.

Parameters
payloadMeasure unit

Definition at line 26 of file haSensor.cpp.

◆ setValueField()

void HASensor::setValueField ( const char *  payload)

Defines a json key that defines sensor value. When setting this, setValueTemplate () should not be called https://www.home-assistant.io/integrations/sensor.mqtt/#value_template.

Parameters
payloadjson key

Definition at line 32 of file haSensor.cpp.

◆ setValueTemplate()

void HASensor::setValueTemplate ( const char *  payload)

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

Parameters
payloadValue template definition

Definition at line 38 of file haSensor.cpp.


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