Help with RESTful params variable

Hi! I am trying to get status info from my new pool, and although there is no current HA integration for it fortunately I found that they do provide an API that talks JSON. So, if I use the following:

curl -X POST -H "Content-Type: application/json" --data '{"pool_api_code":"xxxxx-666666","temperature_scale":0'} https://192.168.0.1/api/poolstatus

I get:

{"pool_spa_selection":1,"heat_cool_selection":1,"temperature":15,"active_favourite":255,"heaters":[{"heater_number":1,"mode":0,"set_temperature":36,"spa_set_temperature":0}],"solar_systems":[],"channels":[{"channel_number":0,"mode":1},{"channel_number":1,"mode":0},{"channel_number":6,"mode":0},{"channel_number":7,"mode":0}],"valves":[],"lighting_zones":[{"lighting_zone_number":0,"mode":0,"color":17}]}

This tells me, amongst other things, that the water temperature is currently 15 degrees celsius and temperature for the heater is set to 36 degrees. To actually see this info in HA I thought that the RESTful integration would be a good way of doing it, but be danged if I can work out how to get the API info in there properly. I am thinking it would be something like:

 sensor:
   - platform: rest
     name: poolsensors
     resource: https://192.168.0.1/api/poolstatus
     params:
     - pool_api_code: !secret pool_api
     - temperature_scale: 0
     scan_interval: 600
     json_attributes:
     - pool_spa_selection
     - heat_cool_selection
     - temperature
     - active_favourite
     - set_temperature

But, and here’s where I need a pointer, I can’t get the “params” bit to work whether I use quotes, parenthesis, or nothing at all. I’m getting various dictionary error messages when testing it, which implies I’m getting the format wrong. I’ve had a hunt and can’t find any examples of anyone using “params” so maybe I’ve wandered off the path of righteousness. I’ve also tried just:

params: '{ "pool_api_code" : "!secret pool_api" }'

but no luck

I don’t have much experience with JSON so I am sure I am missing something obvious… :frowning:

Actually params are sent as query parameters (like ...?pool_api_code=my_pool_api_key) which is not the same as the payload which you used in your curl example. Whether you can send your data as query parameters really depends on the service you use.
If your service only supports data embedded into the request, please have a look at the payload parameter of this integration.

Next, you can’t embed !secret just anywhere. I believe this is only supported at a top-level parameter, e.g. resource: !secret my_secret_resource.

Also, looking at your response JSON and the json_attributes you defined: Everything underneath heaters appears to be defined as an array which means you can’t have set_temperature in the list, but only heaters, and then use for example a template sensor to extract the actual detail from the JSON response.

1 Like

Ah - thanks for all that. I had tried payload, and also only just tried the secret option in my mucking around. I might roll things back to basics and start again after having a closer read of the api docs I just got from the supplier. :slight_smile:

1 Like

Baby steps. Have updated my config to something like:

sensor:
  - platform: rest
    name: poolsensors
    resource: https://192.168.0.1/api/poolstatus
    method: POST
    payload: '{"pool_api_code":"magicnumbers"}'
    scan_interval: 600
    json_attributes:
    - pool_spa_selection
    - heat_cool_selection
    - temperature
    - active_favourite
    - heaters
    - solar_systems
    - channels
    - valves
    - lighting_zones

and it loads without errors. But. On starting I’m now getting a “REST result could not be parsed as JSON” warning in the log, so something else is happening. I might need to wait now until the morning after I’ve had a decent amount of coffee. :wink:

Update - turned on debug and got:

2021-06-01 17:07:03 WARNING (MainThread) [homeassistant.components.rest.sensor] REST result could not be parsed as JSON
2021-06-01 17:07:03 DEBUG (MainThread) [homeassistant.components.rest.sensor] Erroneous JSON: <?xml version="1.0" encoding="utf-8"?>

So, initially looks like the data being returned is not correctly formatted. Dang. Might need two coffees.

Another update:

< HTTP/2 200 
< cache-control: private
< content-type: application/json; charset=utf-8
< server: Microsoft-IIS/10.0
< x-aspnet-version: 4.0.30319
< x-powered-by: ASP.NET

Sigh. Oh, and on top of that I’ve just realised that the output from the server is greater than 255 characters, so that won’t be helping either. I’ll need to look at a workaround (eg json_attributes) in any case.

So, turns out I hadn’t searched hard enough and found someone had already done something similar here. Turns out I just needed to add the headers section. So all good. Now am able to see the temp of the pool as well as turn the lights on or off. Next on my list is seeing if I can make some pretty controls in HA to do things like change the temperature of the heater, and the colour of the lights.