Querying web site from Home Assistant

Hi,

I’m stuck integrating my wallbox (SMA EV charger) into Home Assistant. The only way to access it is web-based.
On the command line, I found out I can get the token required to get the data with this command:
curl http://192.168.8.55/api/v1/token -d "grant_type=password&username=username&password=password##" | jq

… and then use this token to access the information with this command:
curl http://192.168.8.55/api/v1/measurements/live/ -d "[{\"componentId\":\"IGULD:SELF\"}]" -H "Authorization: Bearer <Token>" | jq

This gives following result (shortened for better readability):

[
  {
    "channelId": "Measurement.ChaSess.WhIn",
   [...]
  },
  {
    "channelId": "Measurement.Metering.GridMs.TotWhIn",
    "componentId": "IGULD:SELF",
    "values": [
      {
        "time": "2022-05-19T05:10:59.252Z",
        "value": 398196
      }
    ]
  },
  {
    "channelId": "Measurement.Metering.GridMs.TotWhIn.ChaSta",
    [...]
]

I tried integrating this in a REST sensor in Home Assistant configuration.yaml like this:

rest:
  - resource: http://192.168.8.55/api/v1/measurements/live
    headers:
      Authorization: >
        Bearer {{ states("eyJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2NTI5MzU5MDUsInN1YiI6InRlc3QiLCJ1aWQiOiI3ZWEzZjQzNi1kM2E5LTRjOTktODNjZC1kMDRhNGUyNDQxMzEiLCJleHAiOjE2NTI5Mzk1MDV9.OeVcHEsfaVM7PTKzeBjx-j5zNecIFsLm_IztmELUJPA") }}
    scan_interval: 10
    sensor:
      - name: "Wallbox Total charging"
        value_template: "{{ value_json['Measurement.Metering.GridMs.TotWhIn']['value'] }}"

Unfortunately this does not seem to work. The errors in HASS log don’t help much, the message seems to be cut off:
“Error fetching data: http://192.168.8.55/api/v1/measurements/live failed with”

Second problem in this context:
Since in my experiments the token received in the first request seems to be valid quite a while, I started with the fixed token included in the request to see if I can get it running at all. But I guess to have it fully working I would have to use a dynamic token. Should not be too complicated, but my knowledge is limited to a bit of scripting. How could this be done in Home Assistant? Does this have to be programmed in Python, or is there a much simpler way?

Thanks in advance for any hint that can point me in the right direction!

What are you thinking that your {{ states("eyJ…") }} does?

Your JSON parsing also won’t work like that, but let’s get a valid response first. Try hard-coding the token rather than using a template.

Thank you for the reply. Sorry it took me so long to answer, still fighting with the notification about new posts that did not work…

Blockquote What are you thinking that your {{ states("eyJ…") }} does?

To be honest, I don’t even know where I found this code any more. maybe it was a combination of some code snippet on the internet and some other code I already had in Home Assistant.

Blockquote Try hard-coding the token rather than using a template.

How would I do this, even hard-coded for a first run? I found many issues in this and other forums around integrating a curl command into Home Assistant, but non of them was helpful for a Linux and yaml noob like me. Since you mentioned using a template, is everything inside {{ }} a template? I also tried variations like this:

    headers:
      Authorization: "eyJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2NTI5MzU5MDUsInN1YiI6InRlc3QiLCJ1aWQiOiI3ZWEzZjQzNi1kM2E5LTRjOTktODNjZC1kMDRhNGUyNDQxMzEiLCJleHAiOjE2NTI5Mzk1MDV9.OeVcHEsfaVM7PTKzeBjx-j5zNecIFsLm_IztmELUJPA"

… but that did not seem to change anything.

Just a quick reference: the solution to this was discussed and found in Smart home with SMA PV and wallbox, Wolf heat pump and LEDA wood oven