Migrating HTTP GET of webCore piston to SharpTools Rule

I am using SharpTools connected to my SmarThings hub for my home automation. I am also using the IotaWatt Power Monitor system for which I have been using a webCore piston to access its Query API that uses a restful interface that produces a table of JSON or CSV data.

I am in the process of migrating this webCore piston to SharpTools. To test that things work, I have tried replacing a typical HTTP GET request and assignment to a variable in webCore that works fine shown below:

to what I thought as an equivalent HTTP GET Action and assignment to a SharpTools variable in a SharpTools Rules as shown below:

Unfortunately I do not get any data assigned to the SharpTools variable, whether I use a String or Numeric variable. In the Rule logs on the last command I get that “Set variable value: $TestTotalPowerWatts = undefined”. If I highlight in the Rule listing the $TestTotalPowerWatts variable I get a <> tooltip response and if I highlight the $context.response.data.total_power item, I get a “Available response data.” tooltip response.

Can anyone help?

If you take the URL from your HTTP GET action and put it directly into your browser, what does the response look like?

I noticed that in the WebCoRE version, you were using the raw $response whereas in SharpTools you are trying to access a property of an object within the response (eg. trying to access a total_power JSON property within the response body).

Edit: I also noticed that you mentioned that the API can provide a CSV or JSON response, but in your example it looks like the URL is configured for ‘format=csv’ rather than json?

@josh , thanks for the prompt response.

So, indeed if I put the URL from my HTTP GET action in a browser, the response is a number (the power in Watts), say: 1079.

I have also tried a version of the URL with &format=json in which case the browser response is: [[1079]].

Re your comment: " I noticed that in the WebCoRE version, you were using the raw $response whereas in SharpTools you are trying to access a property of an object within the response (eg. trying to access a total_power JSON property within the response body).". That’s correct, but how else could I assign in the SharpTools Rule the response from the HTTP GET Action to a SharpTools variable? That might solve the problem.

That’s a weird JSON response, but it basically evaluates to an array within an array.

Edit: After reviewing their documentation, it looks like the idea is that it’s a time-series potentially of multiple different series of data which is why it yields an array within an array.

So you could use $context.response.data[0][0] to grab the inner nested value. I believe that would look like the following when you are prompted for which attribute you want to use:

image

A key point here is that the raw response in your browser would have had to look exactly like you mentioned:

[[1079]]

If it was instead an array within an object as their documentation mentions:

{
  "data": [[series1,series2,..],[series1…]]
}

Then you would likely need the input data key to be: data[0][0]

Alternatively, if you just want to assign the raw value from the CSV version you could do that using an expression:

image

Normally, when you assign the response data from an HTTP request, it makes you choose a property as most API responses are structured JSON rather than just raw text, but expressions allow us to access the top-level, raw response data directly. We’re not actually taking advantage of all the neat features of expressions here, but it’s a trick to get the raw data (and it handles implicit conversions to the target variable type).

I have tried the expression from the CSV versions and it is working fine!

Thanks for the prompt and effective response!

1 Like