Fetching MQTT using JSON filter

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
Wiki
Normal user
Posts: 413
Joined: 23 Apr 2018, 17:55
Location: Germany

Fetching MQTT using JSON filter

#1 Post by Wiki » 14 Nov 2022, 15:28

My brandnew Volkszähler using Tasmota sends its data to mosquitto using the format:

Code: Select all

tele/Zaehler/SENSOR = {"Time":"2022-11-14T15:24:00","SML":{"Verbrauch":1.474,"Einspeisung":0.770,"Watt":26.000}}
How can I define a filter in MQTT Import to get hands on the value of "Watt"?

Code: Select all

pi@raspberrypi:~ $ man woman
No manual entry for woman
pi@raspberrypi:~ $

User avatar
Ath
Normal user
Posts: 3515
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Fetching MQTT using JSON filter

#2 Post by Ath » 14 Nov 2022, 17:19

You should enable Parse JSON message by having that option on Yes, set one of 4 'Topic's to "tele/Zaehler/SENSOR" and the corresponding 'JSON Attribute' to "Watt". (all values/names without the quotes ;))
/Ton (PayPal.me)

Wiki
Normal user
Posts: 413
Joined: 23 Apr 2018, 17:55
Location: Germany

Re: Fetching MQTT using JSON filter

#3 Post by Wiki » 14 Nov 2022, 17:51

That's what I've tried the very first, but I can see in the log "tele/Zaehler/SENSOR#Watt=null" resp Watt paylod null, the value of the related field says "nan".
The problem seems to be the "nested" JSON information. Means, the JSON parameters are in double brackets as you can see in my first post. First JSON parameter group (or however its called) is Time, second is "SML".

Setting JSON attribute to SML, index 3, I get the logentry "tele/Zaehler/SENSOR#SML='watt':150}"

Code: Select all

pi@raspberrypi:~ $ man woman
No manual entry for woman
pi@raspberrypi:~ $

User avatar
Ath
Normal user
Posts: 3515
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Fetching MQTT using JSON filter

#4 Post by Ath » 14 Nov 2022, 18:29

Totally untested, but can you set the attribute to SML.Watt ?
/Ton (PayPal.me)

Wiki
Normal user
Posts: 413
Joined: 23 Apr 2018, 17:55
Location: Germany

Re: Fetching MQTT using JSON filter

#5 Post by Wiki » 14 Nov 2022, 19:18

Yes, I've tried almost every configuration with seperation signs, nothing worked.

I've solved the Problem (for me) with a small python script on my 24/7 Raspberry:

Code: Select all

# python 3.8.10

import random
import json

from paho.mqtt import client as mqtt_client

broker = '192.168.xxx.yyy'
port = 1883
topic = "Zaehler/SENSOR"
client_id = f'python-mqtt-{random.randint(0, 100)}'

def connect_mqtt() -> mqtt_client:
    def on_connect(client, userdata, flags, rc):
        if rc == 0:
            print("Connected to MQTT Broker!")
        else:
            print("Failed to connect, return code %d\n", rc)

    client = mqtt_client.Client(client_id)
    client.on_connect = on_connect
    client.connect(broker, port)
    return client

    def subscribe(client: mqtt_client):
        def on_message(client, userdata, msg):
            data=json.loads(msg.payload)
            result = client.publish('Zaehler/SENSOR/Watt',data['SML']['Watt'])
            status = result[0]

    client.subscribe(topic)
    client.on_message = on_message

def run():
    client = connect_mqtt()
    subscribe(client)
    client.loop_forever()

if __name__ == '__main__':
    run()
So I get a clear payload on the topic "Zaehler/SENSOR/Watt" and therefore no problem with importing the data into my tiny little ESP32 and its MQTT Import.

But I would prefer a solution integrated in ESPEasy.

Code: Select all

pi@raspberrypi:~ $ man woman
No manual entry for woman
pi@raspberrypi:~ $

User avatar
Ath
Normal user
Posts: 3515
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Fetching MQTT using JSON filter

#6 Post by Ath » 14 Nov 2022, 20:21

I may have a quick solution for this dual-level JSON data issue, what exact ESP32 build do you use?, so I can provide you with a testbuild ;).

Edit:
Just for fun, I've already compiled an ESP32 Normal build:
ESP_Easy_mega_20221114_normal_ESP32_4M316k.zip
(1.72 MiB) Downloaded 96 times
You can now use the SML.Watt format for the JSON attribute. Only 1 sub-level supported!
/Ton (PayPal.me)

Wiki
Normal user
Posts: 413
Joined: 23 Apr 2018, 17:55
Location: Germany

Re: Fetching MQTT using JSON filter

#7 Post by Wiki » 14 Nov 2022, 22:45

Oh man. Sometimes I'm asking me if you (additionally: TD-er) are doing anything else than supporting ESPEasy.

Your fix works, Thanks a lot.

Code: Select all

pi@raspberrypi:~ $ man woman
No manual entry for woman
pi@raspberrypi:~ $

TD-er
Core team member
Posts: 8752
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Fetching MQTT using JSON filter

#8 Post by TD-er » 14 Nov 2022, 23:32

Wiki wrote: 14 Nov 2022, 22:45 Oh man. Sometimes I'm asking me if you (additionally: TD-er) are doing anything else than supporting ESPEasy.
[...]
What do you think ... ;)

User avatar
Ath
Normal user
Posts: 3515
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Fetching MQTT using JSON filter

#9 Post by Ath » 15 Nov 2022, 21:01

I've created a pull request #4363 for this feature request, and also tossed in some minor string and memory optimizations.
/Ton (PayPal.me)

Post Reply

Who is online

Users browsing this forum: No registered users and 154 guests