RESTful Sensor(s) from API

Hi all

Struggling with trying to create 2 resful sensors from a published API for a uhoo air quality sensor.

the api docs are here: https://documenter.getpostman.com/view/180333/TzeRopef#c9bbd006-2258-4ebf-b56b-b4cae632de3d

so I need to create 2 sensors - the first that uses the ‘https://api.uhooinc.com/v1/generatetoken’ POST request to extract a bearer access token (which only lasts 10 minutes).

then I need to use that bearer token in a 2nd restful sensor that uses the ‘https://api.uhooinc.com/v1/devicedata’ POST to get the sensor values.

Ive tried using the example RESTful sensors on the HA website and adapt them but im getting malformed errors.

Any quick advice on how I can achieve this?

Thanks

just for info - my plan was to trigger sensor 1 every 8 minutes or so, so that the device sensor values always returned a valid access token and therefore provide uninterrupted data. No idea if Im going about thus the right way.

basically I need to learn Json.

Are you able to share the current configuration of your rest sensors (without any personal information)? And could you please share the exact error message? Do you have an issue with both sensors or only one?

1 Like

ok, ive created 3 sensors:

And actually I think they work but im running into ‘invalid token’ with sensors 2 and 3.

could this be related to the formatting of my headers section?

rest:
  - authentication: basic
    scan_interval: 340
    method: POST
    resource: https://api.uhooinc.com/v1/generatetoken
    payload: '{ "code" : "xxxxxxxxxxclientid" }'
    sensor:
      - name: "Uhoo API Key POST"
        json_attributes:
          - access_token
          - refresh_token
          - token_type
          - expires_in

  - authentication: basic
    scan_interval: 3600
    resource: https://api.uhooinc.com/v1/devicelist
    method: GET
    headers:
      Authorization: >
        Bearer {{state_attr ("sensor.uhoo_api_key_post", "access_token")}}
    sensor:
      - name: "Uhoo Device List GET"
        json_attributes:
          - deviceName
          - macAddress
          - serialNumber
          - roomName
          - timezone
          - utcOffset

  - authentication: basic
    scan_interval: 60
    resource: https://api.uhooinc.com/v1/devicedata
    method: POST
    headers:
      Authorization: Bearer {{state_attr ("sensor.uhoo_api_key_post", "access_token")}}
    payload: '{ "macAddress" : "xxxxxxxxxxxx", "mode" : "minute"}'
    sensor:
      - name: "Uhoo Device Data POST"
        json_attributes:
          - temperature
          - humidity
          - pm25
          - tvoc
          - co
          - co2
          - no2
          - ozone
          - airPressure
          - virusIndex
          - timestamp

if anyone sees any obvious errors please let me know -

So far the POST sensor to get the access token works fine - and refreshes based on the scan interval.

the other 2 sensors dont work - atleast I get a ‘invalid token’ error

The first thing I’d try is to keep the Authorization header on a single line, i.e.

Authorization: Bearer {{state_attr ("sensor.uhoo_api_key_post", "access_token")}}

Also, authentication: basic should not be necessary because you are not specifying username or password.

Lastly, the order in which Home Assistant updates these sensors is not in your control. That’s why there is a chance that the two sensors that require the token fail because the sensor that retrieves the token has not been updated yet.
One way to get around that is to use an automation to update the sensors that require the token. You can set the scan_interval to a very large interval and build an automation that checks in a condition if the token is available and only then triggers an update of the two sensors.

thanks - good tips I’ll try that tonight.

i got this working in the end - it was a combination of the api key refreshing every-time I reloaded the rest sensors (doh - ofcourse) and the character limit on the restful integration.

So i used value template to only output one value on the sensor then used attributes for the rest

1 Like

Have browsed through the API documentation a bit, it would be interesting to write a native integration for it so that you can use the data even more easily in Home Assistant :thinking:

But I just need someone I can test the api on.

1 Like

Hey Klaas, still want to do something with this?
I have a Uhoo at home and would like to integrate it in HA.

Yes I am still willing to make an integration for it.

Hello all, conform the documentation you have to get the API from your account at the link uHoo. I however have a premium account (https://premium.getuhoo.com) were I can’t find the API section ti create a Client ID.
Does anybody else has an idea where to get it? If even possible?

Thanks in advance.