HASensorNumber class

class HASensorNumber : public HASensor

HASensorInteger allows to publish numeric values of a sensor that will be displayed in the HA panel.

Note

You can find more information about this class in HASensor documentation.

Public Functions

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

  • precision – Precision of the floating point number that will be displayed in the HA panel.

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

bool setValue(const HANumeric &value, const bool force = false)

Changes value of the sensor and publish MQTT message. Please note that if a new value is the same as the previous one the MQTT message won’t be published.

Parameters
  • value – New value of the sensor. THe precision of the value needs to match precision of the sensor.

  • force – Forces to update the value without comparing it to a previous known value.

Returns

Returns true if the MQTT message has been published successfully.

inline bool setValue(const int8_t value, const bool force = false)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

inline bool setValue(const int16_t value, const bool force = false)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

inline bool setValue(const int32_t value, const bool force = false)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

inline bool setValue(const uint8_t value, const bool force = false)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

inline bool setValue(const uint16_t value, const bool force = false)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

inline bool setValue(const uint32_t value, const bool force = false)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

inline bool setValue(const float value, const bool force = false)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

inline void setCurrentValue(const HANumeric &value)

Sets the current value of the sensor without publishing it to Home Assistant. This method may be useful if you want to change the value before the connection with the MQTT broker is acquired.

Parameters

value – New value of the sensor.

inline void setCurrentValue(const int8_t value)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

inline void setCurrentValue(const int16_t value)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

inline void setCurrentValue(const int32_t value)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

inline void setCurrentValue(const uint8_t value)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

inline void setCurrentValue(const uint16_t value)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

inline void setCurrentValue(const uint32_t value)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

inline void setCurrentValue(const float value)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

inline const HANumeric &getCurrentValue() const

Returns the last known value of the sensor. By default the value is not set.

Protected Functions

virtual void onMqttConnected() override

Private Functions

bool publishValue(const HANumeric &value)

Publishes the MQTT message with the given value.

Parameters

state – The value to publish.

Returns

Returns true if the MQTT message has been published successfully.

Private Members

const NumberPrecision _precision

The precision of the sensor. By default it’s HASensorNumber::PrecisionP0.

HANumeric _currentValue

The current value of the sensor. By default the value is not set.