puuu / ESPiLight

ESPiLight - pilight 433.92 MHz protocols library for Arduino
GNU General Public License v3.0
109 stars 41 forks source link

Is the message formatted properly? #42

Closed akasma74 closed 5 years ago

akasma74 commented 5 years ago

First of all, thanks for your project.

I'm using OMG bridge (Plight module based on this code) (Sonoff RF bridge edition) that sends its data as MQTT messages in the following form: home/OMG_SRFB/PilighttoMQTT {"message":"{\"unitcode\":441825,\"state\":\"opened\"}","protocol":"kerui_D026","length":"","repeats":2,"status":2} home/OMG_SRFB/PilighttoMQTT {"message":"{\"id\":146.0,\"temperature\":1.2,\"humidity\":97.0,\"battery\":0.0}","protocol":"teknihall","length":"146","repeats":3,"status":2}

In an automation (Home Assistant) that is subscribed to this topic I can access top-level pairs like

trigger.payload_json.protocol == 'teknihall'

but the message bit is special, I can't use anything like

trigger.payload_json.message.unitcode

because it's a string, HA does not parse it further.

It looks like HA expects the following pairs in JSON

"key":"string_value"

or

"key": {"key2":"string_value"}
"key":[number, "string", ..]

and the Pilight module runs a message (a python dictionary) through json_encode function that escapes all internal double quotes AND then encloses the value in double quotes to make it

"key":"value"

From what I can see here, a python dictionary represents an object and it should not be enclosed in double quotes.

So am I missing something or the way HA/ESPiLight handles JSONs is not quite correct atm? I'm mainly bothered because currently I have to do a lot of processing just to access that internal message values and that's not right, it shouldn't be that complicated at all.

puuu commented 5 years ago

This is a wrong string and JSON handling in OpenMQTTGateway. A quick look into the OpenMQTTGateway repository showed me that this has already been fixed. Please see 1technophile/OpenMQTTGateway#387.