Publish in MQTT JSON format

Moderators: grovkillen, TD-er, Stuntteam

Post Reply
Message
Author
LeanzLatino
Normal user
Posts: 3
Joined: 19 May 2020, 20:00

Publish in MQTT JSON format

#1 Post by LeanzLatino » 19 May 2020, 20:05

Hey iam trying to figure out how to get publish MQTT json format that homeassistant and many more application use.

Iam using gps plugin and would like to use value in this format.
{"latitude": 50.601972,"longitude": 10.112339, "Altitude": 20.0, "Speed": 2}


Thank you in advance!

User avatar
grovkillen
Core team member
Posts: 3584
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: Publish in MQTT JSON format

#2 Post by grovkillen » 19 May 2020, 20:47

Let's say your task name is "gps" and your value names are the ones you use in your example. Then:

Code: Select all

On gps#latitude Do
Publish,<mqtt topic>,{"latitude": [gps#latitude],"longitude": [gps#longitude], "Altitude": [gps#altitude], "Speed": [gps#speed]}
Endon
Where <mqtt topic> is whatever topic you plan on using.
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

User avatar
dynamicdave
Normal user
Posts: 214
Joined: 30 Jan 2017, 20:25
Location: Hampshire, UK

Re: Publish in MQTT JSON format

#3 Post by dynamicdave » 19 May 2020, 21:07

I thought the json string needed single quotes around the curly brackets... (in the recent ESP Easy releases)

Code: Select all

Publish,<mqtt topic>,'{"latitude": [gps#latitude],"longitude": [gps#longitude], "Altitude": [gps#altitude], "Speed": [gps#speed]}'

User avatar
grovkillen
Core team member
Posts: 3584
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: Publish in MQTT JSON format

#4 Post by grovkillen » 19 May 2020, 21:10

You're right! Thanks for that.
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

LeanzLatino
Normal user
Posts: 3
Joined: 19 May 2020, 20:00

Re: Publish in MQTT JSON format

#5 Post by LeanzLatino » 19 May 2020, 21:30

It doenst seem to change i have also tried.

On gps#latitude Do
Publish,%sysname%,: [gps#latitude],"longitude": [gps#longitude], "Altitude": [gps#Altitude], "Speed": [gps#speed]}
Endon

and also

On gps#latitude Do
Publish,coordinates,: [gps#latitude],"longitude": [gps#longitude], "Altitude": [gps#Altitude], "Speed": [gps#speed]}
Endon

I use same value that wiki recommend , i havent changed to Capital letters etc.
Last edited by LeanzLatino on 19 May 2020, 21:39, edited 1 time in total.

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

Re: Publish in MQTT JSON format

#6 Post by Ath » 19 May 2020, 21:39

LeanzLatino wrote:
19 May 2020, 21:30
It doenst seem to change i have also tried.
Have you tried the example @dynamicdave gave? That uses the quotes around arguments containing spaces or commas, as required in current releases of ESPEasy

Your last examples have invalid JSON

LeanzLatino
Normal user
Posts: 3
Joined: 19 May 2020, 20:00

Re: Publish in MQTT JSON format

#7 Post by LeanzLatino » 19 May 2020, 21:43

Hey
Yes i tried now it sure does work.
Thank you alot!

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 11 guests