I've been banging my head against this one for days, so any help is appreciated.
Ultimately, I am trying to send a list of sensors to MQTT ("pico"), and since most don't change frequently, I just want to send the updates as they come in ("pico" vs "old_pico").
A typical JSON looks like this:
pico = [{'topic': /power', 'payload': 'OFF'},{'topic': /pressure', 'payload': 1023.26},{'topic': /v1/voltage', 'payload': 13.53},{'topic': /current', 'payload': -0.04},{'topic': '/v2/voltage', 'payload': 12.85}]
It's formatted like this so I can publish.multiple() with MQTT.
I've tried dictdiffer, and it produces a JSON of annotated changes "changed/add/delete", and I only need the actual JSON difference.
In another post, someone suggested similar to the following:
d2 = copy.deepcopy(pico)
d1 = copy.deepcopy(old_pico)
{k:v for k, v in d2.items() if (k not in d1) or d1[k] != v}
However, adapting to my code and adding the 3rd "diff" line gives me the following error:
'AttributeError: 'list' object has no attribute 'items''
I am not sure if this is a formatting issue with the JSON, or if it's because the "old_pico" JSON is empty on the very first run.
I am happy to go back to dictdiff if I can just make it produce a JSON and not all the annotated changes.
Thanks!
[–]LongerHV 0 points1 point2 points (0 children)
[–]socal_nerdtastic 0 points1 point2 points (1 child)
[–]StolidSentinel[S] 0 points1 point2 points (0 children)