MQTT JSON problem

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
RayPath
Normal user
Posts: 14
Joined: 01 May 2023, 10:33

MQTT JSON problem

#1 Post by RayPath » 14 May 2023, 11:43

I send via MQTT Broker payload message as JSON :

{"status": "start", "cal": "ZXAB", "gt": "2345"}

In MQTT Import I setup JSON attribute and in log in log ESPEaasy

5083051: IMPT : MQTT fetched json attribute: status payload: start
5083051: IMPT : [MQTT#status] : nan
5083057: IMPT : MQTT fetched json attribute: cal payload: ZX0AB
5083057: IMPT : [MQTT#cal] : nan
5083060: IMPT : MQTT fetched json attribute: gt payload: 2345
5083061: IMPT : [MQTT#gt] : 2345.00
5083062: EVENT: mytopic/test#status=start
5083067: EVENT: MQTT#Status=start
5083072: ACT : OledFramedCmd,Display,on
5083081: EVENT: mytopic/test#cal=ZX0AB
5083087: EVENT: MQTT#Cal=ZX0AB
5083091: EVENT: mytopic/test#gt=2345.00
5083108: EVENT: MQTT#GT=2345.00

on OLED device I put in Lines

1: Status: [MQTT#Status]
2: [MQTT#Cal] > [MQTT#GT]


The ruslt on display is

Status:
> 2345

is not displayad info from MQTT#Cal and MQTT#Status

I see in log that in IMPT lines [MQTT#cal] and [MQTT#status] are nan but in EVENT MQTT#Status and MQTT#Status has values but are not displayed on OLED.
What I am missing ?

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

Re: MQTT JSON problem

#2 Post by Ath » 14 May 2023, 12:41

ESPEasy doesn't store string values when received via MQTT. You can use the Mapping feature of the MQTT Import plugin (when available in the build you are using...) to translate the known strings to a numeric value, and use that to display a state on screen, f.e. "stopped", "started", "running":
1st: Map stopped to 0, started to 1 and running to 2, then in your display definition add:

Code: Select all

Status: {substring:[MQTT#start]*7:6+[MQTT#start]*7:"stoppedstartedrunning"}
And similar for Cal

If it doesn't fit in the Lines definition, you can add it in Rules, adding an event handler for MQTT:

Code: Select all

on MQTT do
  oledframedcmd,1,"Status: {substring:[MQTT#start]*7:7+[MQTT#start]*7:"stoppedstartedrunning"}"
  oledframedcmd,2,"Cal: {substring:[MQTT#cal]*5:5+[MQTT#cal]*5:"ZX0AB??????????"}"
endon
(Air code, untested!)

Then the matching Lines entries should be empty, or the Interval set to 0 so the lines set from rules won't be overwritten.
/Ton (PayPal.me)

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

Re: MQTT JSON problem

#3 Post by Ath » 14 May 2023, 14:00

Now that I'm re-reading my reply, I see there is a better solution.
This involves handling the data in rules exclusively:

Code: Select all

// Handling the data from the JSON data events, as the MQTT events might get translated using a mapping
on mytopic/test#status* do // The * enables non-numeric 1st value processing
  oledframedcmd,1,"Status: %eventvalue1%" // It's crucial to use %eventvalue1% here
endon

on mytopic/test#Cal* do
  oledframedcmd,2,"Cal: %eventvalue1% > [MQTT#GT]" // The GT value is numeric, so will be processed fine
endon
Lines 1 and 2 should be empty in the Device Configuration page, or the data displayed from rules may get overwritten!
/Ton (PayPal.me)

RayPath
Normal user
Posts: 14
Joined: 01 May 2023, 10:33

Re: MQTT JSON problem

#4 Post by RayPath » 14 May 2023, 14:31

Thank you for replay.

I understand that MQTT Import not support string values. but in future it will be nice to have and will be make big value to simple display on DISPLAY
string values which are deliverable are via MQTT like IP address, device name, location etc

Your second suggestion works :-) Thanks

I see how big power are rules work :-) but anyway implement MQTT Import for string will nice to have and simple use only definition
in Device OLED panel what we want display but I understand it is not easy task add support string to MQTT Import

Thanks a lot for your support in reporting problems

Post Reply

Who is online

Users browsing this forum: No registered users and 36 guests