HASensor class

class HASensor : public HABaseDeviceType

HASensor allows to publish textual sensor values that will be displayed in the HA panel. If you need to publish numbers then HASensorNumber is what you’re looking for.

Note

It’s not possible to define a sensor that publishes mixed values (e.g. string + integer values).

Note

You can find more information about this entity in the Home Assistant documentation: https://www.home-assistant.io/integrations/sensor.mqtt/

Subclassed by HASensorNumber

Public Functions

HASensor(const char *uniqueId)
Parameters

uniqueId – The unique ID of the sensor. It needs to be unique in a scope of your device.

bool setValue(const char *value)

Publishes the MQTT message with the given value. Unlike the other device types, the HASensor doesn’t store the previous value that was set. It means that the MQTT message is produced each time the setValue method is called.

Parameters

value – String representation of the sensor’s value.

Returns

Returns true if MQTT message has been published successfully.

inline void setDeviceClass(const char *deviceClass)

Sets class of the device. You can find list of available values here: https://www.home-assistant.io/integrations/sensor/#device-class

Parameters

deviceClass – The class name.

inline void setForceUpdate(bool forceUpdate)

Forces HA panel to process each incoming value (MQTT message). It’s useful if you want to have meaningful value graphs in history.

Parameters

forceUpdate

inline void setIcon(const char *icon)

Sets icon of the sensor. Any icon from MaterialDesignIcons.com (for example: mdi:home).

Parameters

class – The icon name.

inline void setUnitOfMeasurement(const char *unitOfMeasurement)

Defines the units of measurement of the sensor, if any.

Parameters

units – For example: °C, %

Protected Functions

virtual void buildSerializer() final override
virtual void onMqttConnected() override

Private Members

const char *_deviceClass

The device class. It can be nullptr.

bool _forceUpdate

The force update flag for the HA panel.

const char *_icon

The icon of the sensor. It can be nullptr.

const char *_unitOfMeasurement

The unit of measurement for the sensor. It can be nullptr.