Parse nested Json Data from MQTT Broker

hi
since houres of reseach in web to parse the following nested Json String i’am ner to give up.
the Json Strigs is comming from a sonoff POW Device with Tasmota flashed and the content is:
SENSOR = {“Time”:“2023-08-07T08:42:53”,“ENERGY”:{“TotalStartTime”:“2023-02-08T19:32:37”,“Total”:143.027,“Yesterday”:0.464,“Today”:0.166,“Period”:0,“Power”:19,“ApparentPower”:37,“ReactivePower”:32,“Factor”:0.52,“Voltage”:231,“Current”:0.162}}

I want to parse den Wert ApparentPower:37 to display on EPaper display.

I found no code example for nested Json from MQTT. Did someone have made something like this?

thx for some hints

thank you… but sorry, i will do it within ESPHOME

on_json_message:
- topic: Womo/Powermeter/SENSOR
 then:
        -  lambda: | .................

my Yaml ist this:

esphome:
  name: mqtt-json
  friendly_name: mqtt_json

esp32:
  board: esp32dev
  framework:
    type: arduino

globals:
  - id: i_ApparentPower
    type: int

  - id: i_Power
    type: int

mqtt:
  broker: !secret mqtt_ip
  discovery: True
  port: 1883
  username: !secret mqtt_user
  password: !secret mqtt_password

  on_json_message:
    topic: Womo/Powermeter/SENSOR
    then:
      -  lambda: |-
            if (x.containsKey("Time")){
              if (x["Time"] > 100) {
              ESP_LOGD("on_json_message", "Found");
              const char *ApparentPower = x["ApparentPower"];
              const char *Power = x["Power"];
              id(i_ApparentPower) = *ApparentPower;
              id(i_Power) = *Power;
              }
              else ESP_LOGD("on_json_message", "nothing found");
            }
                 
# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "OYAf+xxSHy4nmqfn2KarfrHLVggmhRSyr29+LCPupSE="

ota:
  password: "36e84a752cd9db833275157e079d1aa9"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Mqtt-Json Fallback Hotspot"
    password: "mjnuaiOn9Y8u"

and the log:

INFO ESPHome 2023.6.3
INFO Reading configuration /root/config/mqtt-json.yaml...
INFO Starting log output from mqtt-json.local using esphome API
INFO Successfully connected to mqtt-json.local
[13:55:54][I][app:102]: ESPHome version 2023.6.3 compiled on Aug  7 2023, 13:20:28
[13:55:54][C][wifi:543]: WiFi:
[13:55:54][C][wifi:379]:   Local MAC: 9C:9C:1F:E3:A4:50
[13:55:54][C][wifi:380]:   SSID: [redacted]
[13:55:54][C][wifi:381]:   IP Address: 192.168.1.123
[13:55:54][C][wifi:383]:   BSSID: [redacted]
[13:55:54][C][wifi:384]:   Hostname: 'mqtt-json'
[13:55:54][C][wifi:386]:   Signal strength: -58 dB ▂▄▆█
[13:55:54][C][wifi:390]:   Channel: 6
[13:55:54][C][wifi:391]:   Subnet: 255.255.255.0
[13:55:54][C][wifi:392]:   Gateway: 192.168.1.1
[13:55:54][C][wifi:393]:   DNS1: 192.168.1.1
[13:55:54][C][wifi:394]:   DNS2: 0.0.0.0
[13:55:54][C][logger:301]: Logger:
[13:55:54][C][logger:302]:   Level: DEBUG
[13:55:54][C][logger:303]:   Log Baud Rate: 115200
[13:55:54][C][logger:305]:   Hardware UART: UART0
[13:55:54][C][mdns:108]: mDNS:
[13:55:54][C][mdns:109]:   Hostname: mqtt-json
[13:55:54][C][ota:093]: Over-The-Air Updates:
[13:55:54][C][ota:094]:   Address: mqtt-json.local:3232
[13:55:54][C][ota:097]:   Using Password.
[13:55:54][C][mqtt:127]: MQTT:
[13:55:54][C][mqtt:129]:   Server Address: 192.168.1.252:1883 (192.168.1.252)
[13:55:54][C][mqtt:130]:   Username: [redacted]
[13:55:54][C][mqtt:131]:   Client ID: [redacted]
[13:55:54][C][mqtt:133]:   Discovery prefix: 'homeassistant'
[13:55:54][C][mqtt:134]:   Discovery retain: YES
[13:55:54][C][mqtt:136]:   Topic Prefix: 'mqtt-json'
[13:55:54][C][mqtt:138]:   Log Topic: 'mqtt-json/debug'
[13:55:54][C][mqtt:141]:   Availability: 'mqtt-json/status'
[13:55:54][C][api:138]: API Server:
[13:55:54][C][api:139]:   Address: mqtt-json.local:6053
[13:55:54][C][api:141]:   Using noise encryption: YES
[13:56:34][D][on_json_message:036]: nothing found

the mqtt message arrived bun ther is no parsing…

Can you read your own code? :wink:
There is parsing:

  1. Because it enters on_json_message
  2. Because if (x.containsKey("Time")) evaluates to true

Comparing a string with an int?

yes, your are right. the tow questions are how to :

  1. make a correct containsKey condition?
  2. how to read the content one layer deeper for ApperentPower :
    {“Time”:“2023-08-07T14:16:33”,“ENERGY”:{“TotalStartTime”:“2023-02-08T19:32:37”,“Total”:142.903,“Yesterday”:0.464,“Today”:0.042,“Period”:0,“Power”:20,“ApparentPower”:39,“ReactivePower”:33,“Factor”:0.53,“Voltage”:234,“Current”:0.165}}
    you have a idea?

I have not clue what “containsKey” you want nor what you’re trying to achieve with Time.

ApparentPower is

x["ENERGY"]["ApparentPower"]

no-nonsense lambda (untested):

            if (x.containsKey("ENERGY"))
            {
                ESP_LOGD("on_json_message", "Found");
                const char *ApparentPower = x["ENERGY"]["ApparentPower"];
                const char *Power = x["ENERGY"]["Power"];
                id(i_ApparentPower) = *ApparentPower;
                id(i_Power) = *Power;
            }
            else 
              ESP_LOGD("on_json_message", "nothing found");

thank you, the yaml is ok to compiling and uploaded. The Log shows a good message:

[19:49:43][C][mqtt:134]:   Discovery retain: YES
[19:49:43][C][mqtt:136]:   Topic Prefix: 'mqtt-json'
[19:49:43][C][mqtt:138]:   Log Topic: 'mqtt-json/debug'
[19:49:43][C][mqtt:141]:   Availability: 'mqtt-json/status'
[19:50:34][D][on_json_message:030]: Found
[19:50:42][I][mqtt:286]: MQTT Connected!
[19:50:42][I][app:062]: setup() finished successfully!
[19:50:42][I][app:102]: ESPHome version 2023.6.3 compiled on Aug  7 2023, 16:34:11
[19:50:42][C][wifi:543]: WiFi:

now i try to print the data in the log to check the function.