hobbyquaker / homekit2mqtt

HomeKit to MQTT bridge 🏡📱
MIT License
349 stars 71 forks source link

Option/Ability to ignore missing values in JSON #129

Open sillyfrog opened 6 years ago

sillyfrog commented 6 years ago

Thanks for #67 - just started using it now! :)

Much like the original posts on #67, I'm using Tasmota. I have a dimmable light, but unfortunately Tasmota will often output the following:

stat/light/RESULT {"POWER":"ON","Dimmer":54,"Color":"00000089","HSBColor":"0,0,0","Channel":[0,0,0,54]}
stat/light/RESULT {"POWER":"ON"}

It will also output just the second line if only the power was switched (and the Dimmer was not changed).

Would it be possible to have an option to ignore updates if the value is not present in the JSON. For example, I'm currently matching on just Dimmer - but if there was some sort of alternate syntax supported (just an idea), say ..Dimmer - if the value was not in the JSON, rather than treating it as 0 or false it would just ignore it, and treat it as if it was not sent.

Thanks again!

hobbyquaker commented 6 years ago

Hmm, that's not so easy to accomplish, I'm caching the payload of all incoming topics, so if an iOS device issues a HAP Get I'm reading the value from that cache. But when a payload with a missing property overwrites the previous entry in the cache I have no possibility to answer on a Get request for the then missing property - because homekit2mqtt has no knowledge of the previous payload(s) - in your example I can't give an answer to a Get on the dimmer value after {"POWER":"ON"} overwrote the cache. It's not impossible - but it's huge effort (first idea: keeping a distinct cache for all characteristics). Will think about it, maybe I come up with a easier solution, give me some time.

sillyfrog commented 6 years ago

Another thought on this, what if it merged any incoming JSON data with existing data? I'm extremely naive when in comes to node, so may not work. I'm thinking something such as this: https://stackoverflow.com/questions/14974864/combine-or-merge-json-on-node-js-without-jquery

I've tried to figure it out in the code quickly, but not getting too far, if you have some quick pointers where the incoming messages are saved, I can try and see if I can come up with a patch that'll work.

Thanks again!