Hi,
the goal I try to achieve is to send all raw data received on the IR receiver (remote control) to an mqtt broker. This is how far I got:
esphome:
name: simple01
platform: ESP32
board: esp32dev
# Enable logging
logger:
# Enable Home Assistant API
api:
ota:
password: "fc3bea7671d0b9f89ef9b9612db66e5b"
wifi:
ssid: "xxx"
password: "xxx"
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Board01 Fallback Hotspot"
password: "xxx"
mqtt:
broker: 192.168.178.67
birth_message:
topic: iot/sensor01
payload: "going online"
will_message:
topic: iot/sensor01
payload: "going offline"
id: mymqtt
remote_receiver:
id: myreceiver
pin: GPIO15
dump: all
on_raw:
then:
- mqtt.publish:
topic: iot/sensor01
payload: !lambda return x;
captive_portal:
The mqtt.publish_json part is not working, getting error:
config/simple01.yaml:42:14: error: could not convert 'x' from 'std::vector<int>' to 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}'
Disclaimer: Beginner, so please be forgiving
tom_l
July 25, 2021, 10:33am
2
I had the opposite problem. I needed to convert a string to a vector. The solution was provided by the kind and knowledgeable people on the ESPHome discord channel:
- uart.write:
id: projector
data: !lambda |-
std::string str = command;
std::vector<uint8_t> vec(str.begin(), str.end());
return vec;
I’m just following my nose here and trying to reverse the solution. Try this:
payload: !lambda |-
std::vector<uint8_t> vec = x;
std::string str(vec.begin(), vec.end());
return str;
If it does not work I suggest you pop over to discord and ask. https://discord.gg/UX45SJuh
Hi,
thanks but that does not seem to work, not sure if I found the best solution but in the end this is what is working for me:
on_raw:
then:
- mqtt.publish:
topic: iot/sensor01
payload: !lambda |-
reverse(x.begin(), x.end());
int decimal = 1;
int total = 0;
for (auto& it : x)
{
total += it * decimal;
decimal *= 10;
}
return esphome::to_string(total);
1 Like
martser
(Martin)
August 6, 2023, 4:21pm
4
Hello, how i do it using on_aeha, the problem is that when i add on_aeha, on_raw… etc… i do not see the mesages in the log anymore, i would like to use the code publishing on text_sensor for address and another text sensor for data, so i can use immediately the code to test and avoid the modification in YAML.
Thanks