Read the statement by Michael Teeuw here.
Correct json syntax (MMM-Json-Table)
-
@raymondjspigot said in Correct json syntax (MMM-Json-Table):
Hi @raymondjspigot - thank you so much for your reply. Very helpful.
You are right re: your first code “window” formatting - that is almost exactly what I see. I now also realise that it is in XML format, rather than json (but I hadn’t when I posted my OP). And you are correct that I need to change the XML in to json.
This is all at the edge of my knowledge, but how might I “translate” the XML in to json? By using Node-RED perhaps? I already use NR for another project, but if so, I am not sure how I would then send on the re-formatted message back in to MMM-Json-Table, as does the latter not expect to receive the information from a webpage, rather than from a programme (such ass NR)?
That last bit might not even be a sensible question, but I’ll ask it anyway :face_with_stuck-out_tongue_winking_eye:
-
OK, I have some progress, in that a very slightly amended url now returns data in json format (I think!). Here is an example of what comes back …
{"overview":{"lastUpdateTime":"2020-09-30 10:35:31","lifeTimeData":{"energy":1162278.0,"revenue":247.86499},"lastYearData":{"energy":1126618.0},"lastMonthData":{"energy":450237.0},"lastDayData":{"energy":1600.0},"currentPower":{"power":1234.6772},"measuredBy":"INVERTER"}}
I have altered my MMM config to be
{ module: 'MMM-JsonTable', position: 'top_right', header: 'Solar Panels', config: { url: 'https://monitoringapi.solaredge.com/%20site/REDACTED/overview.json?api_key=REDACTED', arrayName: "overview", updateInterval: '60000', } },
But whilst the config.js “runs”, it returns an error on the mirror screen saying “Json data is not of type array! Maybe the config arrayName is not used and should be, or is configured wrong”.
Is there anything obvious that I have done wrong?
TIA
-
hi @grinly that’s good progress persuading it to respond with a json message anyway. It looks like what you’re getting is now more like
{ "overview": { "lastUpdateTime":"2020-09-30 10:35:31", "lifeTimeData": { "energy":1162278.0, "revenue":247.86499 }, "lastYearData": { "energy":1126618.0 }, "lastMonthData": { "energy":450237.0 }, "lastDayData": { "energy":1600.0 }, "currentPower": { "power":1234.6772 }, "measuredBy":"INVERTER" } }
which does look much more json-ish. Unfortunately your inverter’s not providing the power and energy data in an array[] format. In json an array would look more like the Example 1 message - it would be essentially a named list (called items in the example) and show a set of readings, each providing the same kind of datapoints, and contained in the message between [ and ]. I’m guessing that the error you’re seeing is because you’ve provided
arrayName: "overview"
in your config.js, so the module’s looking in the json for an array with that name, and there isn’t one (there’s a json element overview, but it’s not an array.)
I haven’t tried using the MMM-JsonTable module so I don’t know for sure - do you get anything displayed by the module if you request the inverter data in json format again but then leave the arrayName blank in your config.js (or comment-out the arrayName entry)?
-
@raymondjspigot Thanks once again for your reply. Even with
arrayName: "overview"
commented out it returns the same error on screen.From other research, I think that half of my problem is that the url returns “nested” data, which MMM-JsonTable doesn’t like. I did look to work around the problem another way, by getting Node-RED to send the API url, and then “clean up” the message in to the bits I wanted; whilst I could do that, I then ran in to the problem of how to get this back in to MMM-JsonTable :anguished_face:
Ho Hum, the delights of programming! (But I won’t give up just yet …)
-
hi @grinly no worries : )
I agree, I think MMM-JsonTable doesn’t like the structure of the json message you’re getting back - it’s expecting to see information laid out in an array and isn’t set up to parse a different message structure.
Your investigations into Node-RED sound interesting - it’s not something I’ve tried but just from a quick look you might well be able to get it to act as an intermediary between your inverter and MagicMirror. If you’ve managed to get Node-RED to call the inverter endpoint for the energy data then that’s a good start. You’re right, if you go this route I think you’ll still have to clean up/reformat the message for consumption by MMM-JsonTable. But the Node-RED documentation might help - it does look like there are ways to get data back to the module too - you could perhaps get MMM-JsonTable to call a Node-RED endpoint instead of the inverter directly, and Node-RED could serve “cleaned-up” json back to MM?
https://cookbook.nodered.org/http/serve-json-content
Another option might be to see if there’s an alternative MagicMirror module that can handle (or be configured to handle) a wider range of json (or XML) messages? Though you might have to get hands-on with a module to adjust it exactly the way you wanted. If your json message always follows the same format it shouldn’t be too hard to extract the values you’re interested in - I think there are some fairly straightforward modules that would give you an outline of how to request and parse json (e.g. MMM-stocks maybe?). If you wanted the output shown in a table you could take a look inside MMM-JsonTable and see how it’s done there too. Just scrolling the 3rd-party modules list there’s also MMM-Tabulator? I’ve not used this one either but it mentions displaying data from XML or json sources, so it might be worth a try.