Getting sensor state data as string into the rest platform

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.

Thanks for the suggestion.

That doesn’t work either unfortunately.

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…

Thx
James

Do you mean that line?
{{ 'Bearer ' + states.sensor.agl_auth.state }}
Just copy an paste it in Developer Tools -> Templates, and you see the result.

Thanks - I just gave that a go and it looks like there are a few line breaks in there. So I’ll have to figure out how to remove them…
JP

Just rechecked it and no line breaks.

I suspect that the rest sensor doesn’t support templating :frowning:

Does anyone have any other thoughts?

When I use this line;
> Authorization: {{ 'Bearer ' + states.sensor.agl_auth.state }}

HASS won’t even boot. It gives me this;

17-04-07 12:32:51 ERROR (Thread-1) [homeassistant.util.yaml] invalid key: "OrderedDict([('states.sensor.agl_auth.state', None)])"
  in "/home/hass/.homeassistant/configuration.yaml", line 169, column 0
17-04-07 12:32:51 ERROR (MainThread) [homeassistant.bootstrap] Error loading /home/hass/.homeassistant/configuration.yaml: invalid key: "OrderedDict([('states.sensor.agl_auth.state', None)])"
  in "/home/hass/.homeassistant/configuration.yaml", line 169, column 0

JP

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?

  ...
  unit_of_measurement: "°F"
  data_template:
    headers:
      X-Auth-Token:  '{{ states.sensor.auth.state }}'
  ...
  unit_of_measurement: "°F"
  headers:
    X-Auth-Token:  '{{ states.sensor.auth.state }}'

Hi @azeroth12

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.

Thx
James

Hey @jjpeet
I actually just got done going another way.

  1. Put my sensor.yaml into a folder.
  2. 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)
  3. Use the secret in the rest sensor header

Also ugly but seems to work!

Hi @azeroth12

Actually that sounds better than mine as it avoids a restart (I’m assuming). Can you share your config of both secret & config files?

Thx
James

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):

- platform: rest
  resource: 'https://tappecue.babyvelociraptor.com/session/1/1'
  name: "Probe 1"
  method: GET
  value_template: '{{ value_json.current_temp }}'
  unit_of_measurement: "°F"
  headers:
    X-Auth-Token: !secret tappecue

shell_command (scrapes the id from the login response and creates the secrets.yaml file with the new auth token):

  tappecue_auth: 'curl -s -d "username=[username]&password=[pass]" https://tappecue.babyvelociraptor.com/login | grep -m 1 ":" | cut -c20-55 | perl -ne ''print "tappecue: $_"'' > /Users/server/.homeassistant/sensors/secrets.yaml'

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).

Not in my experience.

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…

JP

1 Like

Thank you for confirming my observations.

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

2 Likes

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 :wink:

Hi, old topic, but do you have some examples do get container info from Portainer api?
I want a switch to restart a container I created on Portainer

I’ve switched to using HA dockermon for monitoring and controlling my docker containers, rather than relying on the Portainer API.

Oh, yes that’s another road… You didn’t succeed on Portainer API?

No, working with a Bearer token which expires after 8 hours was just too complicated.

Ok, thnx for feedback, appreciated