When I call this sensor It appears that a carriage return is being placed between Bearer and the key and causes authentication to fail. It looks something like this:
I may be wrong, but I get the feeling you are looking in the wrong place for why it fails. I suspect the token is placed on the next line where you looked at it because it was wordwrapped. I cannot tell (and probably you neither) because you are showing yaml notations, but the header isn’t yaml so we’re not looking at the real thing.
So the first question should be, what is actually ending up in the rest call. The first thing that seems off is the indenting in the first code block, but that might be a pasting error because you didn’t post the whole thing. The second thing is you do not use the state_attr function, but what you did should work as far as I can tell as long as there’s no error. So these are probably not it. I’m not sure if the authentication key supports templating in the first place though. I think it should though.
Seeing what the developer tools display for the template as koying suggested is the first step. Putting that hard coded in the rest api to see if it works is the next step. If that works the template isn’t expanded (or isn’t expanded right), if it doesn’t then something else is wrong. So forst get it working with hard coded bearer token, then proceed.
This posts reports similar issues, suggesting templating is possible… but: unexpected for yaml, they use an Authorization header attribute with a Capital A. Could that be it? It is what the header needs it to be:
It is also what the docs said (where we both should have looked first )
I thought I had it solved but I’m still getting this error
2024-09-08 10:27:56.748 INFO (MainThread) [homeassistant.components.sensor] Setting up rest.sensor
2024-09-08 10:27:56.748 DEBUG (MainThread) [homeassistant.components.rest.data] Data fetched from resource: {“timestamp”:“2024-09-08T15:27:56.264+00:00”,“status”:401,“error”:“Unauthorized”,“message”:“The token is invalid: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $”,“path”:"