Announcement

Collapse
No announcement yet.

Help turning http API request message into values

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Help turning http API request message into values

    I've got a Solax inverter which has a web api i can call. I thought I could tweak a friend's flow for weather underground but on investigation the returned values are not in an array.

    when I make a web request i get the following:

    {"success":true,"exception":"Query success!","result":{"inverterSN":"xxxxxxxxxxxxx","sn":"xxxxx xxxxxxx","acpower":1329.0,"yieldtoday":3.0,"yieldtotal":183. 2,"feedinpower":578.0,"feedinenergy":31.97,"consumeenergy":7 1.02,"feedinpowerM2":0.0,"soc":30.0,"peps1":0.0,"peps2":null ,"peps3":null,"inverterType":"15","inverterStatus":"102","up loadTime":"2023-04-25 10:44:01","batPower":2170.0,"powerdc1":3472.0,"powerdc2":0.0 ,"powerdc3":null,"powerdc4":null,"batStatus":"0"}}

    The code i was trying to use was processing this return from WeatherUnderground and seems to be already set up as an array called observations.

    {"observations":[{"stationID":"xxxxxxxxxx","obsTimeUtc":"2023-04-25T09:45:04Z","obsTimeLocal":"2023-04-25 10:45:04","neighborhood":"xxxxxxxxxxxxxxxxxxxxx","softwareTy pe":null,"country":"GB","solarRadiation":623.0,"lon":0.754," realtimeFrequency":null,"epoch":xxxxxxxxxxxx,"lat":xxxxxxxxx xx,"uv":5.3,"winddir":23,"humidity":68.0,"qcStatus":1,"uk_hy brid":{"temp":7.9,"heatIndex":7.9,"dewpt":2.3,"windChill":6. 0,"windSpeed":10.8,"windGust":12.2,"pressure":1032.17,"preci pRate":0.00,"precipTotal":0.00,"elev":23.0}}]}

    I was looking for a way to take my http return and turn it into an array so that i can assign the values to HS devices but I'm just not getting it even when using split/html edit node or converting to JSON.

    I might be missing something obvious but banging my head up against the wall atm.

    #2
    I know this is a Node Red forum but as a FYI, this could easily be done with datascraper.
    Jon

    Comment


      #3
      Jon00 to the rescue 😀. I'll probably do that initially now that you have reminded me. At some point i will need to be able to remotely set the battery charging times dependant on current charge but one step at a time. I'm enjoying node red so far, when i turn the shield on for a movie it will turn on the sub, switch the avr input and up the volume and switch it all back again when i turn it off.

      I've also gotten fed up with the HS Alexa addin and now run all the voice integration through node red flows.

      Comment


        #4
        More than one way to skin a cat...chose what you are most comfortable with!
        Jon

        Comment


          #5
          Originally posted by jon00 View Post
          More than one way to skin a cat...chose what you are most comfortable with!
          Totally agree, just want to get a bit better with node red at the same time. Thanks again!

          Comment


            #6
            There are 2 invalid numbers, otherwise that is a JSON string. Running it through a JSON formatter/Validator gave me the result shown at the bottom of this post.

            In order to deal with this, we can use a Change node and JSONata with a replace() function and regex. Since one of the numbers is a decimal and my regex skills are minimal, I need to do 2 replacements on your data. For completeness where the decimal may also follow the space, I added a third. Here is how I set the Change Node up for a test: The 3 JSONata calls are:
            $replace(payload, /(\d+)(.)(\s+)(\d+)/,"$1$2$4")
            $replace(payload, /(\d+)(\s+)(.)(\d+)/,"$1$3$4")
            $replace(payload, /(\d+)(\s+)(\d+)/,"$1$3")



            Click image for larger version

Name:	image.png
Views:	75
Size:	30.9 KB
ID:	1611595​



            Test Flow

            Click image for larger version

Name:	image.png
Views:	79
Size:	8.9 KB
ID:	1611594​


            Test Flow to Import.
            Code:
            [
                {
                    "id": "5f7f452fc76d378b",
                    "type": "inject",
                    "z": "86bfba1f202dac0c",
                    "name": "",
                    "props": [
                        {
                            "p": "payload"
                        },
                        {
                            "p": "topic",
                            "vt": "str"
                        }
                    ],
                    "repeat": "",
                    "crontab": "",
                    "once": false,
                    "onceDelay": 0.1,
                    "topic": "",
                    "payload": "{\"success\":true,\"exception\":\"Query success!\",\"result\":{\"inverterSN\":\"xxxxxxxxxxxxx\",\"sn\":\"xxxxx xxxxxxx\",\"acpower\":1329.0,\"yieldtoday\":3.0,\"yieldtotal\":183. 2,\"feedinpower\":578.0,\"feedinenergy\":31.97,\"consumeenergy\":7 1.02,\"feedinpowerM2\":0.0,\"soc\":30.0,\"peps1\":0.0,\"peps2\":null ,\"peps3\":null,\"inverterType\":\"15\",\"inverterStatus\":\"102\",\"up loadTime\":\"2023-04-25 10:44:01\",\"batPower\":2170.0,\"powerdc1\":3472.0,\"powerdc2\":0.0 ,\"powerdc3\":null,\"powerdc4\":null,\"batStatus\":\"0\"}}",
                    "payloadType": "str",
                    "x": 130,
                    "y": 1640,
                    "wires": [
                        [
                            "d415b332c9ab51fe"
                        ]
                    ]
                },
                {
                    "id": "e3b662a016a46869",
                    "type": "debug",
                    "z": "86bfba1f202dac0c",
                    "name": "debug 51",
                    "active": true,
                    "tosidebar": true,
                    "console": false,
                    "tostatus": false,
                    "complete": "true",
                    "targetType": "full",
                    "statusVal": "",
                    "statusType": "auto",
                    "x": 860,
                    "y": 1640,
                    "wires": []
                },
                {
                    "id": "e7b75aed49815e46",
                    "type": "json",
                    "z": "86bfba1f202dac0c",
                    "name": "",
                    "property": "payload",
                    "action": "",
                    "pretty": false,
                    "x": 590,
                    "y": 1640,
                    "wires": [
                        [
                            "e3b662a016a46869"
                        ]
                    ]
                },
                {
                    "id": "d415b332c9ab51fe",
                    "type": "change",
                    "z": "86bfba1f202dac0c",
                    "name": "",
                    "rules": [
                        {
                            "t": "set",
                            "p": "payload",
                            "pt": "msg",
                            "to": "$replace(payload, /(\\d+)(.)(\\s+)(\\d+)/,\"$1$2$4\")",
                            "tot": "jsonata"
                        },
                        {
                            "t": "set",
                            "p": "payload",
                            "pt": "msg",
                            "to": "$replace(payload, /(\\d+)(\\s+)(.)(\\d+)/,\"$1$3$4\")",
                            "tot": "jsonata"
                        },
                        {
                            "t": "set",
                            "p": "payload",
                            "pt": "msg",
                            "to": "$replace(payload, /(\\d+)(\\s+)(\\d+)/,\"$1$3\")",
                            "tot": "jsonata"
                        }
                    ],
                    "action": "",
                    "property": "",
                    "from": "",
                    "to": "",
                    "reg": false,
                    "x": 360,
                    "y": 1640,
                    "wires": [
                        [
                            "e7b75aed49815e46"
                        ]
                    ]
                }
            ]​

            Results of JSON Validator on original data
            Click image for larger version

Name:	image.png
Views:	92
Size:	72.1 KB
ID:	1611593​

            Karl S
            HS4Pro on Windows 10
            1070 Devices
            56 Z-Wave Nodes
            104 Events
            HSTouch Clients: 3 Android, 1 iOS
            Google Home: 3 Mini units, 1 Pair Audios, 2 Displays

            Comment


              #7
              I had some time to do some testing and found that this is a better regex and only requires the one entry in a Change node:
              $replace(payload, /(\d+\.*)(\s+)(\.*\d+)/,"$1$3")

              \.* escapes the . which has other meaning in regex and the * means zero or more occurrences. So make the Change Node use this and remove the other 2 entries and you should be good. Provided, of course, that we needed to remove those spaces. If they have meaning, let me know what and we can try to figure out what is needed.
              Click image for larger version

Name:	image.png
Views:	85
Size:	18.1 KB
ID:	1611714​
              Karl S
              HS4Pro on Windows 10
              1070 Devices
              56 Z-Wave Nodes
              104 Events
              HSTouch Clients: 3 Android, 1 iOS
              Google Home: 3 Mini units, 1 Pair Audios, 2 Displays

              Comment

              Working...
              X