Greetings
It’s me again, feels like it at least. I just cannot wrap my head around the more advanced lamda stuff.
This time I’m trying to have ESPhome parse the below MQTT message from a temperature sensor sent by node-red and set the value of a global variable to “temperature.value” from the message.
I just cannot understand how to get to the specific value. For another project I managed to parse an MQTT value that wasn’t “hidden” in a JSON but this time I’m lost.
What I want to achieve as a first step is something below, at least have the logger print the temperature value when there is a change but the output is empty.
This is proving to be pretty convoluted, I found this thread maybe you’ll see something I didn’t
I set this up on my system and was trying the following but something is off an it wont compile
globals:
- id: temperature
type: float
- id: humidity
type: float
# Enable Home Assistant API
mqtt:
broker: 192.168.0.101
on_json_message:
topic: test/temp
then:
- lambda: |-
if (x.containsKey("temperature")){
if (x["temperature"] == 25) {
ESP_LOGD("on_json_message", "Found");
float temperature = x["temperature.value"];
float humidity = x["humidity.value"];
id(temperature) = temperature;
id(humidity) = humidity;
}
}
/config/esphome/fan-controller.yaml: In lambda function:
/config/esphome/fan-controller.yaml:34:20: error: base operand of '->' is not a pointer
id(temperature) = temperature;
^
/config/esphome/fan-controller.yaml:35:17: error: base operand of '->' is not a pointer
id(humidity) = humidity;
^
Compiling /data/fan-controller/.pioenvs/fan-controller/lib790/FS/vfs_api.cpp.o
*** [/data/fan-controller/.pioenvs/fan-controller/src/main.cpp.o] Error 1
Hi
Convoluted indeed. Thank you for your time. I was under the impression the process of extracting a nested JSON in an MQTT message was common practice and it was just me not understanding things.
Anyway in the meantime I have made a workaround where I set node-red, who is sending the MQTT in the first place, to also send the temperature value as a simple value which is working with the below ESP code.
In HA it is. I have a lot of mqtt sensors in HA from nested JSON and it was straight forward setting them up. Lambda’s are C++ which is a traditional programming language unlike yaml that uses “human readable” commands.
I just want to mention that they have made it really easy to bring in values from HA with the api. For example I use an input_number to adjust a delay in esphome.