-
Notifications
You must be signed in to change notification settings - Fork 183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
decode JSON data from message #176
Conversation
Thanks for this, which sounds useful. I don't think we can take it as it stands, though, because you're clobbering the passed title from the configuration; furthermore, you're not protecting against the payload not containing JSON. However, I'll wait to see what @sumnerboy12 says about this. |
Yes I agree with @jpmens - what you need to do is write a simple function which pulls out the attributes from your JSON payload and updates the By doing this it also means the message can be delivered to any See here for details. |
OK, obviously I'm doing something wrong here. I would be very happy if you show me the right way, @sumnerboy12. I tried breaking this down to the most simple problem, so I try using it without any function or template (I already used a template to form the message for XBMC), and without my phone system. This is the relevant part from my mqttwarn.ini:
Having that configured, I try using it like this:
This results in XBMC displaying a message with the title To find out what's going on here, I simply printed the item in
This made me think that the JSON has do be decoded before title and message can be extracted. It seems to work for @sumnerboy12, so my hope that this pull-request isn't necessary seems to be fulfilled. I just would like to learn how to use this module. Aside from that, I agree with @jpmens: I should have protected against non-JSON messages. And I probably should have tried decoding |
You will need a custom function, which when called for this target can decode the JSON payload, extract the title and message, and update the dict (you printed above). There is an example on the page I linked above, which may not be exactly what you want but you should get the drift. By default there is no post processing of the payload, and it is copied across as the |
OK, I'm beginning to feel a little stupid, here. Even more than usual... Now I tried a function. To keep it simple, I try setting fixed values for title and message:
In mqttwarn.ini I configured this:
Instead of In a desperate try, I even tried returning a fixed string from my function:
As expected, this is in 'item.message' when I use Can you please show me how to write the function so that I can overwrite Oh, and I really want to thank you for dealing with my problems: Thanks a lot! |
Ok, taking a step back, forgetting about functions, can you try this;
|
Thank you very much! I really didn't get the concept how this is supposed to work. Well, I'm not sure if I got it now, but: it works! I thought that the function or the jinja2-template (that's what I tried before even creating the pull request) are supposed to create the title and the message that end up in In case somebody else has the same problem (or if anybody wants to show me that I'm still doing it wrong), I'll just show what I did: This lives in my
And in
Finally, I'm happy with this software again. I'm sure I will have much more fun with it in the future. |
Glad you got there in there end! |
I don't have much experience with MQTT or with mqttwarn, so I'd be happy to learn that I'm doing something wrong and this pull isn't necessary. However, it works for me. :-)
I configured an XBMC target in mqttwarn.ini. I want to send a message with a title and an image, as shown in #53 (got my home phone system to talk MQTT, by the way):
mosquitto_pub -t xbmc/command/notify -m '{"message": "Call from <number>", "title": "Phone is ringing!", "image": "/storage/phone-icon.png"}'
XBMC shows a notification, but the message text contains the undecoded JSON data. In xbmc.py, this data is delivered in item.message, so I wrote these few lines to decode it.
Again: this works for me. I didn't find another way, I'd like to see a working example.