ttc71
(Thomas)
October 10, 2023, 6:59pm
1
Hello,
I want to retrieve a bearer token from an auth rest url and store it in a sensor. However, the token is longer than 255 characters and I get an error message.
Therefore I want to split the token (length 876 characters) to different sensors and reassemble it later.
Unfortunately I do not get further at this point:
sensor:
- name: "Solarman API Access Token Bearer 1"
value_template: "Bearer{{ value_json.access_token }}"
name: "Solarman API Access Token Bearer 2"
value_template: "Bearer {{ value_json.access_token }}"
How could I cut the token after 255 characters and then save the next part?
msp1974
(Mark P)
October 11, 2023, 12:19am
2
Use this
{{var_name[start:end]}}
So something like this
sensor:
- name: "Solarman API Access Token Bearer 1"
value_template: "Bearer{{ value_json.access_token[:255] }}"
name: "Solarman API Access Token Bearer 2"
value_template: "Bearer {{ value_json.access_token[256:] }}"
123
(Taras)
October 11, 2023, 12:34am
3
If you save the bearer token in an attribute, you won’t need to split it. An attribute doesn’t have a 255 character limit.
sensor:
- name: "Solarman API Access Token Bearer"
value_template: "{{ now().isoformat() }}"
attributes:
token: "Bearer{{ value_json.access_token }}"
You can access the token
attribute using the state_attr()
function.
ttc71
(Thomas)
October 11, 2023, 3:14pm
4
I have my code inside rest.yaml
, and now this works with your hint:
- resource: 'https://globalapi.solarmanpv.com/account/v1.0/token?appId=<ID>&language=en&='
method: POST
scan_interval: 60000
headers:
Content-Type: application/json
payload: '{"appSecret": "<secret>","email": "<mail>","password": "<PW>"}'
sensor:
- name: "Solarman API Access Token Bearer"
value_template: "{{ now().isoformat() }}"
unique_id: ssensor.solarman_api_access_token_bearer
json_attributes:
- access_token
- resource: 'https://globalapi.solarmanpv.com/device/v1.0/currentData?appId=<ID>&language=en&='
method: POST
scan_interval: 60
headers:
Authorization: "bearer {{ state_attr('sensor.solarman_api_access_token_bearer', 'access_token') }}"
Content-Type: application/json
payload: '{ "deviceSn": "<Id>"}'
sensor:
- name: "DC Voltage PV1"
unit_of_measurement: V
value_template: "{{ value_json.dataList[9]['value'] }}"
unique_id: sensor.rest_solarman_pv1_voltage
....