HTTP Request string

I am trying to extract information from my Fronius solar inverter. I can issue an HTTP request using by PV System ID, and it returns a single-line string as such:

{"IsOnline":true,"AllOnline":true,"P_Grid":-3298.6199999999999,"P_Load":-3029.9343355941773461,"P_Akku":8.8155660629272461,"P_PV":6319.73876953125,"SOC":95.599999999999994,"BatMode":1.0,"Ohmpilots":[],"Wattpilots":[],"Consumers":[],"Generators":[]}

I am trying to manipulate this string so I can break out the relevant components into individual variables.

In SharpTools, I can issue the HTTP GET request successfully, but I can’t work out how to get the whole string (or the components) into a variable.

$context.response.status returns 200 showing that the command executed successfully.

When I select Variable->Context Variable->Response->HTTP->Response Data, it then asks me to input an object.property. Typing the variable names in the string (e.g. “P_PV”) doesn’t work, and I can’t seem to just ask for the whole thing. How do I get the whole string?

I have tried using (), “”, #, * and any other version of a null I can think of.

Thanks in advance.

Here’s my test rule so far. It’s the $context.response.data variable I am having trouble with.

Here’s what the return string looks like in a browser

P_PV looks like a valid property.

So the full variable would look like $context.response.data.P_PV

What do the Rule Logs show for the details under the entry where you’re setting the variable value?

You can do it in an Expression. You can use:

$context.response.data
1 Like

As a test, I just took the payload from your first post and put it into a GitHub Gist to simulate the response (intentionally causing it to have an incorrect ‘text’ content type to test that too) and the rule executed as expected:

Source URL for reference:

https://gist.githubusercontent.com/joshualyon/090f0ba237726dcf5b4f83b7c392ce40/raw/f6a516cd4b34ee21b24eb6dc4e3d7d0a83529e8a/test.txt

Rule Log showing the raw response as well as the P_PV property get copied over fine:

And the rule flow itself…

(I used a random numeric variable I had tested with in the past to store the P_PV value. $AATmp is a text variable to store the raw response.)

If you’re still having difficulty, feel free to send a note to support@sharptools.io with the Rule ID and I would be happy to take a closer look with you. :slight_smile:

1 Like

Thank you Josh. I have worked it out. Using the expression $context.response.data I was able to see that it was not, in fact, returning the XML string, rather the default Fronius webpage.

I needed to add a cookie from my login as a header in the GET request so it new it was me. Now working as expected.

1 Like

Hi Josh, is it possible to extract the values from the text variable once you have stored the http response there? In your example above, how would you parse the P_PV value form variable $AATmp?

I have managed to do it with a nested round(mid($TmpResponse,indexOf($TmpResponse,“P_PV”)+7,6)) expression. Was just wondering if there were a simpler way to parse out the variables.

Hi Mike - thanks for sharing your solution. Another approach would be to parse the stored data back into an object so you can reference the value directly using parseJson().

response = parseJson($TmpResponse)
response["P_PV"]

The challenge with using substrings is the position and length of the content might change. It looks like you solved the position part of things with the indexOf() method (nice move!) which leaves you with the potential for the length of the content to change.

1 Like