MagicMirror² v2.13.0 is available! For more information about this release, check out this topic.

Correct json syntax (MMM-Json-Table)



  • A url with my details in returns the following data about my solar panels;

    
    2020-09-25 17:06:53
    
    1109407.0
    236.23314
    
    
    1073747.0
    
    
    397366.0
    
    
    10979.0
    
    
    278.9876
    
    INVERTER
    
    

    If that doesn’t display well on this forum, I have also put a screen grab on DropBox https://www.dropbox.com/s/x2larxqazl4vau7/Screenshot 2020-09-25 at 17.38.02.png?dl=0

    The module https://github.com/timdows/MMM-JsonTable shows an example of the config as follows:

    {
    	module: 'MMM-JsonTable',
    	position: 'top_right',
    	header: 'HouseDB Sevensegment',
    	config: {
    		url: 'https://xyz/abc/get.json', // Required
    		arrayName: 'items' // Optional
    	}
    }
    

    I would be most grateful if someone could help me sort out the correct syntax to use within the module? Just one dataset would be fine, and then I can work from there…

    TIA



  • Hi @grinly - so (apologies - just to make sure what you’re looking for …)

    From your screenshot it looks like you’re receiving an XML message back from your inverter (hopefully this comes out ok - something like -

    <overview>
      <lastUpdateTime>2020-09-25 17:06:53</lastUpdateTime>
      <lifeTimeData>
        <energy>1109407.0</energy>
        <revenue>236.23314</revenue>
      </lifeTimeData>
      <lastYearData>
        <energy>1073747.0</energy>
      </lastYearData>
    etc.
    

    Whereas I think the MMM-JsonTable module is looking for a json response to parse. Are you wanting to transform the XML message you have into json first, so that MMM-JsonTable can work with it? The raw json message in Example 1 on https://github.com/timdows/MMM-JsonTable shows a simple structure you could aim for, e.g.

    {
        "items": [
            {
                "name": "Lifetime Energy",
                "value": "1109407"
            },
            {
                "name": "Lifetime Revenue",
                "value": "236.23314"
            },
            {
                "name": "Energy Last Year",
                "value": "1073747"
            },
            {
                "name": "Last item in my example array for now",
                "value": "3 or something"
            }
        ]
    }
    
    

    which gives MMM-JsonTable an array named “items” (hence arrayName : ‘items’ in the config.js for the module - so you can change the array name as long as the config entry changes to match) and each entry in the items array has 2 datalabel:datavalue pairs in. You can see the syntax - the array entries go between [ and ], and each entry gets its own { and } with a trailing comma between them.

    Does this help any? Hopefully I haven’t misunderstood what you’re trying to achieve 🙂



  • @raymondjspigot said in Correct json syntax (MMM-Json-Table):

    https://github.com/timdows/MMM-JsonTable

    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 😜



  • 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 😧

    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.


Log in to reply