ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
mqtt_subscribe_sensor.cpp
Go to the documentation of this file.
2
3#ifdef USE_MQTT
4
5#include "esphome/core/log.h"
6
8
9static const char *const TAG = "mqtt_subscribe.sensor";
10
13 this->topic_,
14 [this](const std::string &topic, const std::string &payload) {
15 auto val = parse_number<float>(payload);
16 if (!val.has_value()) {
17 ESP_LOGW(TAG, "Can't convert '%s' to number!", payload.c_str());
18 this->publish_state(NAN);
19 return;
20 }
21
22 this->publish_state(*val);
23 },
24 this->qos_);
25}
26
28void MQTTSubscribeSensor::set_qos(uint8_t qos) { this->qos_ = qos; }
30 LOG_SENSOR("", "MQTT Subscribe", this);
31 ESP_LOGCONFIG(TAG, " Topic: %s", this->topic_.c_str());
32}
33
34} // namespace esphome::mqtt_subscribe
35
36#endif // USE_MQTT
void subscribe(const std::string &topic, mqtt_callback_t callback, uint8_t qos=0)
Subscribe to an MQTT topic and call callback when a message is received.
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
mopeka_std_values val[3]
MQTTClientComponent * global_mqtt_client
constexpr float AFTER_CONNECTION
For components that should be initialized after a data connection (API/MQTT) is connected.
Definition component.h:55
optional< T > parse_number(const char *str)
Parse an unsigned decimal number from a null-terminated string.
Definition helpers.h:1143