But the problem is that I need to update the token every couple of hours.
Getting the token works. If I go to Developer -> States -> rest_token I can see the correct value and the token updating. I am just trying to figure out how to get that value into the other rest sensors authentication variable.
Unfortunately this won’t work, because the headers can only be strings, not templates.
I’m not aware of any way to accomplish what you are trying to do inside Home Assistant. Does your server support any other authentication method, or supports adding the token as URL parameter?
And as a side note: it would probably have been better to share your progress on this topic on your previous post from 2 days ago, instead of asking almost the same question again in a new post.
After a lot of mucking around I have managed to figure it out.
Step 1: Create a secret in secrets.yaml
# Use this file to store secrets like usernames and passwords.
# Learn more at https://www.home-assistant.io/docs/configuration/secrets/
some_password: welcome
rest_token:
Step 2: Create a command_line sensor which updates the secrets.yaml rest_token value
This step was a lot of mucking around trying to convert the command to something yaml would accept
Actual shell command (for testing via ssh):
You should get the access token printed to the terminal screen and the secrets.yaml rest_token value updated
edit:
needed command [&& ha core restart] to be added to command in rest_token to restart ha after token update so the rest sensors use the new token secret.
I basically have this same problem.
I have a refresh token that doesn’t expire, and using that I get a JWT that’s valid for 12 hours. That JWT goes in a header in the rest sensor that gets the data.
I’ve made a script that updates the JWT and stores it in the secrets, but now I just realized that the value doesn’t get re-read by the rest sensor until Home-Assistant restarts… And I don’t want to restart every 12 hours, because of course that would always occur exactly when my wife’s doing something with it and then I’ll get the whole “I told you this smart home crap will never work” lecture
So anyway… The way that seems the best would be to create another Rest sensor for the token value. But I haven’t managed to get that to work - the documentation says the headers should accept templates, and that could be used to get the value, but it just doesn’t seem to work… Also it’s a pain in the ass to debug because HASS needs to be restarted for the changes to take effect. I guess I’ll have to look into this again now that I’m a bit more familiar with templates.