This works and gives me what I’m after but only lasts for about 24 hours - that’s how long the security token lasts for. I then have to manually go and grab the new token and update my config and restart HASS.
So I wrote a shell script which I run from command line sensor which returns a new token every twelve hours to the sensor state as follows;
Hi @jjpeet, have you tried Authorization: "{{ 'Bearer ' + states.sensor.agl_auth.state }}"
or if templating is not working with the headers, why not make a command line sensor that does the complete job.
I’m reluctant to put the whole thing in the command line sensor because then it would be renewing the auth token every minute or so which I’d prefer to avoid.
Is there a way to echo or log within HA what that line is delivering? If I wasn’t flying blind so much I could probably debug it…
Did you ever get this to work? I’m in the exact same boat with trying to get the token guid from a command line sensor.
The sensor works fine and has the correct token but I can’t seem to get the rest sensor to accept a template for the header value.
I’ve tried both of these with no errors in the log but no value is returned. (A value is returned if I hardcode the token guid) Any ideas?
No I didn’t get it to work and instead took a different approach.
I’m using a shell script to grab the token via curl, write this to a sensor YAML file, and then restart HA at 6am every morning.
Not particularly elegant, but it does the job. HA will hang on restart normally once a month or so (I think it gets stuck on wemo), but that’s not too bad given I had to do it manually every day before!
Let me know if you want the detail and I’ll put it together and post here for you.
Hey @jjpeet
I actually just got done going another way.
Put my sensor.yaml into a folder.
Have the shell script (no longer a sensor) write out the token key to secrets.yaml in the new script folder. (only secret in the file as the script overwrites each time)
Yep no restart required. So my use case is for a grilling wifi thermometer. The shell_command creates/replaces a secrets.yaml file in the new sensor folder each time it’s called with the active session id. Probably could have done something fancier to append/replace in my main secrets.yaml, but I didn’t feel like putting in the effort for that part. You can call the shell_command with a time based automation or I’ll probably do it with a manual script as I’m not grilling 24/7…
sensor (uses the local secrets.yaml to get the active auto token):
Can anybody confirm that using a secret actually works if the secret changes, without restarting Home Assistant?
I’ve been playing with RESTful sensors to talk to Portainer and obtain some information about my docker containers. The Portainer API uses Bearer authentication, and the token expires after 8 hours. I’m using a bash script (via cron) to retrieve a new token every hour, and write it to a secrets.yaml file in my sensors folder. When I restart Home Assistant, it picks up the current secret and my RESTful sensors work just fine. But 7 to 8 hours later, the sensors show up with an unknown state and the Home Assistant log is full of errors (because the JSON response can no longer be processed, it holds an authentication error instead).
I use a bash script which uses curl to grab a new token every 24 hours. I then write out a new secrets.yaml file for my template sensor - which is ignored by HA until I do a reboot. So my script also restarts HASS.
That approach has been working reliably for the last 6 months…
I’m thinking writing a module might be the only way to make it work, but I don’t have the time (or skills) for that at the moment…
Argh, I’m starting to wonder if trying to talk to Portainer is even worth the trouble. The list of things I have to find a solution for:
can’t use a changing Bearer token in the headers of the RESTful sensor (and I’m obviously not going to restart Home Assistant every 8 hours), could be resolved by supporting templating in the headers
the RESTful swith (which I could use for starting/stopping docker containers) doesn’t support headers at all at the moment
can’t get json_attributes to work on the RESTful sensor (probably due to a bug)
Everytime I make some progress, I hit a new snag. [/rant]
I’m currently using the information (link below) to put together my own pool pump automation. It’s a great pattern to follow to create a custom component.
It got me thinking that potentially I could replace my bash script we’re talking about with a custom made component instead. That would handle the token refresh, data ingest and creation of custom sensors for the data I get from my AGL Solar Command website… Just a thought…
JP
I’ll definitely look into that a little more when I have some time. The thought already crossed my mind to take the existing RESTful sensor/switch and modify it as a custom component. The only problem is I am not a coder, and certainly not a Python coder, so it would require a combination of research, trial and error to get it to work. Not that I normally let that stop me