Hello,
I am trying to save input from an mqtt topic to a global variable, so I can use it later. Unfortunately there seems to be a problem with encoding.
My config.yml looks like this:
esphome:
name: test
esp8266:
board: d1_mini
# Enable logging
logger:
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Example configuration entry
mqtt:
broker: !secret mqtt_host
username: !secret mqtt_user
password: !secret mqtt_password
on_json_message:
topic: test
then:
- globals.set:
id: test_var
value: !lambda |-
ESP_LOGD("main", "test_mqtt: %s", x["0"].as<const char*>());
return x["0"].as<const char*>();
globals:
- id: test_var
type: const char*
restore_value: no
initial_value: '"Init"'
interval:
- interval: 1sec
then:
- lambda: |-
ESP_LOGD("main", "test_interval: %s", id(test_var));
I am using docker to compile and upload it.
sudo docker run --rm --privileged -v "${PWD}"/config:/config --device=/dev/ttyUSB0 -it ghcr.io/esphome/esphome run test.yaml
I’ve logged the variables value to debug. Output is:
[12:45:32][D][main:038]: test_interval: Init
[12:45:33][D][main:038]: test_interval: Init
[12:45:33][D][main:025]: test_mqtt: test1234
[12:45:34][D][main:038]: test_interval:
[12:45:35][D][main:038]: test_interval:
[12:45:36][D][main:038]: test_interval:
[12:45:37][D][main:038]: test_interval:
[12:45:38][D][main:038]: test_interval:
[12:45:39][D][main:038]: test_interval:
[12:45:39][D][main:025]: test_mqtt: test1234
[12:45:40][D][main:038]: test_interval: [\xd5\xecKY\xbd\xf2$
[12:45:41][D][main:038]: test_interval: m
[12:45:42][D][main:038]: test_interval:
[12:45:43][D][main:038]: test_interval:
[12:45:44][D][main:038]: test_interval:
[12:45:45][D][main:038]: test_interval:
[12:45:45][D][main:025]: test_mqtt: test12345
[12:45:46][D][main:038]: test_interval:
[12:45:47][D][main:038]: test_interval:
I published multiple test messages to my test topic. You can see the value being properly logged for test_mqtt, but I can’t get the proper value from the var.
Does somebody have an idea about what I am doing wrong? Thanks a lot. Help is highly appreciated.