Restful Sensor Gives No State Value - No idea what i am doing wrong - PLEASE HELP!

I am trying to use vaules from an API i have tested the API with postman and the data structure i recieve back is below (irrelevant values removed but structure maintained)

{
    "data": {
        "api_token": "fd3b6366e28fdc77e8c12fe8ff2a7425",
    },
    "devices": [
        {
            "did": "KlkvwRNNnJNQC4bEnE1uSf",
        }
    ],
    "message": "Authorization Successful!"
} 

I then use the following inside “/homeassistant/configuration.yaml”

rest:
  - resource: "https://MYURL/login"
    scan_interval: 172000
    method: POST
    headers:
      Content-Type: application/json
    payload: '{
      email: "[email protected]",
      password: "MYPASS"
    }'
    
    sensor:
      - unique_id: 22c32b5b-8c0c-4995-8a38-2838f78ac147
        name: "hottub_login_api"
        value_template: "{{ value_json }}"

          
      - unique_id: ba9f0dcb-6c49-47c6-b6d1-c4a0c9900a32
        name: "hottub_login_did"
        value_template: "{{ value_json }}"

When i lookup the sensor in developer tools it has no value

previous to this i have also tried the followng but they always result in the sensor being in an unavalible state

    sensor:
      - unique_id: 22c32b5b-8c0c-4995-8a38-2838f78ac147
        name: "hottub_login_api"
        value_template: "{{ value_json['data']['api_token'] }}"

-----OR-----
    sensor:
      - unique_id: 22c32b5b-8c0c-4995-8a38-2838f78ac147
        name: "hottub_login_api"
        value_template: "{{ value_json.data.api_token }}"

-----OR-----
    sensor:
      - unique_id: 22c32b5b-8c0c-4995-8a38-2838f78ac147
        name: "hottub_login_api"
        value_template: "{{ value_json }}"
        json_attributes_path: "$.data"
        json_attributes:
          - api_token

Please help, is my initial rest not working am i missing somthing from the post, how can i debug the initial POST or is there somthing else i am doing wrong?

value_template: "{{ value[:205] }}"

Sets the state to plain text response so you can see what is really being returned with your call. It stays under the state length limit in case the response is longer than that.

Thanks looks like my POST is not authenticated so i need to look at what is causing that.

UPDATE - i found there was too much whitespace in my payload

it now looks like

rest:
  - resource: "https://MYURL/login"
    scan_interval: 172000
    method: POST
    payload: '{"email":"[email protected]","password":"MYPASS"}'
    
    sensor:
      - unique_id: 22c32b5b-8c0c-4995-8a38-2838f78ac147
        name: "hottub_login_api"
        value_template: "{{ value_json }}"

Cheers

1 Like