Rest sensor availability issue

I have a few rest sensors that each have a different resource and each one relies on another sensor state being available.

In my scenario, first I have a sensor which does a POST to retrieve a bearer token. Next I have a sensor which does a GET and uses the token in its header for the request to retrieve an API key. Lastly I have a sensor which does another GET and users the key in its header to get the data I need.

The first sensor works perfectly since it doesn’t rely on anything else but the other two show as unknown unless I trigger the update entity service manually via developer tools.

Is there a way to have the second sensor wait for a value from the first and so on?

You could try using a command line sensor instead, or doing something with templating.

Any ideas how that would work with a command line sensor?

Here’s a command line script I use for updating my printer internal clock that does something similar to get a cookie to allow uploading of a config file. Might help…

#!/bin/bash
curl -c cookie.txt -kv -u [ADMIN_USER]:[ADMIN_PASS] https://192.168.1.9/#hId-dateTimePage > /dev/null 2>&1
# all manner of commands to build hp-time.xml
curl -k -b cookie.txt -H "Content-Type:text/xml" -T hp-time.xml -u [ADMIN_USER]:[ADMIN_PASS] --url "https://192.168.1.9/DevMgmt/ProductConfigDyn.xml"

Thanks for sharing that. The part I can’t figure out is I need to take the returned json output from my first curl and use it as a header in the second curl.

In that case, you need to direct the output of curl to a file, then use that file. In my first command, I’m dumping the output as I don’t need it.

Can you share an example, with anything critical redacted?

It does seem a bit clunky to store outputs in files for the next curl to use. I was looking at shell commands but from my understanding they would need to be triggered using automations as where I want to create a sensor (see usage case below).

My use case is to get consumption readings from my power company which are available via their website and then dump them into a sensor for use in HA. They don’t expose any APIs to get the data so I had to work at it a bit via the browser tools to retrieve the specific calls, headers, keys etc.

My first curl is a POST which uses my credentials to get the bearer token.
Next is a GET where I pass the bearer token in the header which gives me the API key.
Then one more POST where I pass the bearer token and API key to get the power consumption readings.

Thought I would share my own solution in the hope that it will help someone else down the track.

  • I ended up leaving each request as its own rest sensor to keep things simple
  • The first rest sensor which authenticates runs once an hour and outputs the bearer token.
  • The second and third rest sensors are configured with a very high scan internal - about 1 year (reasoning below)
  • I created an automation which does the following:
    • Triggers when the first sensor state changes
    • Calls the homeassistant.update_entity service for the second rest senor to return the API key
    • Waits 5 seconds
    • Calls the homeassistant.update_entity service for the third rest sensor to return the energy data I want