ESPHome 2025.5.0
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
7namespace esphome {
8namespace mqtt_subscribe {
9
10static const char *const TAG = "mqtt_subscribe.sensor";
11
14 this->topic_,
15 [this](const std::string &topic, const std::string &payload) {
16 auto val = parse_number<float>(payload);
17 if (!val.has_value()) {
18 ESP_LOGW(TAG, "Can't convert '%s' to number!", payload.c_str());
19 this->publish_state(NAN);
20 return;
21 }
22
23 this->publish_state(*val);
24 },
25 this->qos_);
26}
27
29void MQTTSubscribeSensor::set_qos(uint8_t qos) { this->qos_ = qos; }
31 LOG_SENSOR("", "MQTT Subscribe", this);
32 ESP_LOGCONFIG(TAG, " Topic: %s", this->topic_.c_str());
33}
34
35} // namespace mqtt_subscribe
36} // namespace esphome
37
38#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:39
mopeka_std_values val[4]
MQTTClientComponent * global_mqtt_client
const float AFTER_CONNECTION
For components that should be initialized after a data connection (API/MQTT) is connected.
Definition component.cpp:27
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
optional< T > parse_number(const char *str)
Parse an unsigned decimal number from a null-terminated string.
Definition helpers.h:313