I am trying to collect state of a switch on other ESPHome device using rest.
Here is the code
- http_request.get:
url: http://esp8266-esp-01.local/switch/relay2
verify_ssl: false
on_response:
then:
- logger.log:
format: 'Response status: %d'
args: [status_code]
- lambda: |-
const char* httpResponse = id(http_request_data).get_string();
ESP_LOGD("http_response", "Response %s", httpResponse);
json::parse_json(httpResponse, [](JsonObject root) {
ESP_LOGD("http_parse", "id: %s", root["id"]);
ESP_LOGD("http_parse", "state: %s", root["state"]);
});
Response recevied is OK. This is what response contains:
{"id":"switch-relay2","value":true,"state":"ON"}
It seems as proper json, but parsing goes wrong. I get gibberish.
Here is excerpt from the log my code produces:
[11:15:48][D][main:490]: Get status
[11:15:48][D][main:499]: Response status: 200
[11:15:48][D][http_response:115]: Response {"id":"switch-relay2","value":true,"state":"ON"}
[11:15:48][D][http_parse:118]: id: @
[11:15:48][D][http_parse:119]: state: @
[11:15:48][D][http_request:094]: HTTP Request completed; URL: http://esp8266-esp-01.local/switch/relay2; Code: 200
If state in json is OFF then all values are parsed as āDā instead of ā@ā. While i can play with @ and D to recognize status that is plain wrong. I want to get proper value.
Am I doing something wrong or there is some issue with json::parse_json()?