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 Types

enum Features

Values:

enumerator DefaultFeatures
enumerator JsonAttributesFeature

Public Functions

HASensor(const char *uniqueId, const uint16_t features = DefaultFeatures)
Parameters
  • uniqueId – The unique ID of the sensor. It needs to be unique in a scope of your device.

  • features – Features that should be enabled for the sensor.

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.

bool setJsonAttributes(const char *json)

Publishes the given JSON data on the JSON attributes topic. The JsonAttributesFeature has to be enabled prior to setting the value.

Parameters

json – JSON attributes.

void setExpireAfter(uint16_t expireAfter)

Sets the number of seconds after the sensor’s state expires, if it’s not updated. By default the sensors state never expires.

Parameters

expireAfter – The number of seconds.

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 setStateClass(const char *stateClass)

Sets class of the state for the long term stats. See: https://developers.home-assistant.io/docs/core/entity/sensor/#long-term-statistics

Parameters

stateClass – 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 uint16_t _features

Features enabled for the sensor.

const char *_deviceClass

The device class. It can be nullptr.

const char *_stateClass

The state class for the long term stats. It can be nullptr. See: https://developers.home-assistant.io/docs/core/entity/sensor/#long-term-statistics.

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.

HANumeric _expireAfter

It defines the number of seconds after the sensor’s state expires, if it’s not updated. By default the sensors state never expires.