Frans-Willem / AqaraHub

AqaraHub is an open-source Zigbee hub for Xiaomi Aqara devices. It aims to be a replacement to the Xiaomi Gateway that does not require communication to outside servers, and uses a saner communication option (e.g. MQTT).
252 stars 34 forks source link

JSON vs. plain values in MQTT #34

Closed mclei-asw closed 6 years ago

mclei-asw commented 6 years ago

Continuing on issue #24:

About the choice between of plain values instead of JSON, I've considered it in the past, but decided against it for symmetry. e.g. when receiving a value from Zigbee, it's obviously easy to just serialize it as the plain value, but then when trying to send something from MQTT to Zigbee 1 could be interpreted as a uint8, int8, uint16, uint24, ..., single, float, double, and there's really no way to know which one the zigbee device will support.

It is ok for me to use JSON for sending commands, as it could be "constructed" as simple strings, without any JSON support. But for reading received values I have to use some JSON parser.

I know that OpenHAB supports javascript (and thus JSON) transforms, and Domoticz needs some kind of translation like Node-Red inbetween anyway that can tackle the JSON stuff, which is why I figured it wouldn't be a very big problem. May I ask what kind of home automation setup you'd like to run that wouldn't support JSON?

Now, the primary master of my home automation is PLC with IEC61131-3 programming (Foxtrot from Teco), where I have programmed a MQTT client. Parsing JSON is possible there, but it adds a unnecessary overhead because it is limited, specially in string resources.

Next I use some Arduino/ESP based gadgets, all connected to MQTT broker. And there I found it as a big overhead.

mclei-asw commented 6 years ago

Another reason: If I want to control Sonoff sockets directly through MQTT, then I also need 0/1 values and not JSON.

Frans-Willem commented 6 years ago

I'll see what I can do after I get the big refactor needed to get water sensor & co working :)

Frans-Willem commented 6 years ago

I've also done some work on this in the branch for [https://github.com/Frans-Willem/AqaraHub/issues/24](issue #24). If you pass the command-line argument "--recursive-publish", it will recursively publish JSON object properties & array elements to MQTT sub-topics. For example the Xiaomi button, on single press, would report to these topics:

<INFO> [PublishValue] Publishing to 'AqaraHub/00158d000152d7b2/1/in/OnOff/Report Attributes': {"reports":[{"Attribute data":{"type":"bool","value":true},"Attribute identifier":"OnOff"}]}
<INFO> [PublishValue] Publishing to 'AqaraHub/00158d000152d7b2/1/in/OnOff/Report Attributes/reports': [{"Attribute data":{"type":"bool","value":true},"Attribute identifier":"OnOff"}]
<INFO> [PublishValue] Publishing to 'AqaraHub/00158d000152d7b2/1/in/OnOff/Report Attributes/reports/0': {"Attribute data":{"type":"bool","value":true},"Attribute identifier":"OnOff"}
<INFO> [PublishValue] Publishing to 'AqaraHub/00158d000152d7b2/1/in/OnOff/Report Attributes/reports/0/Attribute data': {"type":"bool","value":true}
<INFO> [PublishValue] Publishing to 'AqaraHub/00158d000152d7b2/1/in/OnOff/Report Attributes/reports/0/Attribute data/type': "bool"
<INFO> [PublishValue] Publishing to 'AqaraHub/00158d000152d7b2/1/in/OnOff/Report Attributes/reports/0/Attribute data/value': true
<INFO> [PublishValue] Publishing to 'AqaraHub/00158d000152d7b2/1/in/OnOff/Report Attributes/reports/0/Attribute identifier': "OnOff"
<INFO> [OnZclCommand] Looks like something per-attribute. Publishing per-attribute too
<INFO> [PublishValue] Publishing to 'AqaraHub/00158d000152d7b2/1/in/OnOff/Report Attributes/OnOff': {"type":"bool","value":true}
<INFO> [PublishValue] Publishing to 'AqaraHub/00158d000152d7b2/1/in/OnOff/Report Attributes/OnOff/type': "bool"
<INFO> [PublishValue] Publishing to 'AqaraHub/00158d000152d7b2/1/in/OnOff/Report Attributes/OnOff/value': true

It'll be a bit 'chatty' on MQTT, but I think this will give you the most possibilities to listen for things without having to parse JSON. e.g. even the things that report bit-arrays (e.g. [true, false, false, false]) where you only need the first element, will get a specific subtopic for each element in that array.

Let me know if this solves your problem :)

mclei-asw commented 6 years ago

It is really too chatty, but it seems it simply works. I should test what is the impact on the mqtt server.

Frans-Willem commented 6 years ago

Current master version supports the --recursive-publish flag, which I think solves this. Closing for now :)