Restful sensors dependant on another sensor value - not working

Using the rest function i have created a couple of sensors to collect authentication data (this has been changed).

I then want to use this authentication data to query the status in another status, if i add the data manually to the payload then i get returned data but i if i use this data from the states i fail authentication

My “/homeassistant/configuration.yaml”

rest:
  - resource: "https://MY-LOGIN-URL/login"
    scan_interval: 172000
    method: POST
    payload: '{"email":"[email protected]","password":"MYPASS"}'
    
    sensor:
      - unique_id: 288cf0be-36d5-4462-8630-9c89b1a6c044
        name: hot_tub_all_data
        value_template: "{{ value[:205] }}"
        
      - unique_id: 2e1ee29e-6ec2-4733-8732-66e8e4ad3e1e
        name: "hot_tub_login_api"
        value_template: "{{ value_json.data.api_token }}" ##example returns (f5354cf4251832edc3b26d82b465c646)
         
      - unique_id: b49e9102-63d0-44a0-9eed-a22a39c4f800
        name: "hot_tub_login_did"
        value_template: "{{ value_json.devices[0].did }}" ##example returns (KlkvwRNNnJNQC4bEnE1uSf)
        
      - unique_id: 2d1bfa11-bba1-42a4-bca0-feba9605f019
        name: "hot_tub_login_payload"
        value_template: '{"did":"{{states.sensor.hot_tub_login_did.state}}","api_token":"{{states.sensor.hot_tub_login_api.state}}"}'

  - resource: "https://MY-STATUS-URL/status"
    scan_interval: 10
    headers:
        Content-Type: application/json
    method: POST
{{states.sensor.hot_tub_login_api.state}}}'
    payload: '{"did":"KlkvwRNNnJNQC4bEnE1uSf","api_token":"f5354cf4251832edc3b26d82b465c646"}'
    
    sensor:
      - unique_id: cc2c04b2-4837-4599-b002-c682f504971a
        name: "hot_tub_status_all"
        value_template: "{{ value[:205] }}"

The above returns status data form the API

however if i do etiher of the following the request is not authenticated

  - resource: "https://MY-STATUS-URL/status"
    scan_interval: 10
    headers:
        Content-Type: application/json
    method: POST
    payload: '{"did":{{states.sensor.hot_tub_login_did.state}},"api_token":{{states.sensor.hot_tub_login_api.state}}}'
    sensor:
      - unique_id: cc2c04b2-4837-4599-b002-c682f504971a
        name: "hot_tub_status_all"
        value_template: "{{ value[:205] }}"

---OR---

  - resource: "https://MY-STATUS-URL/status"
    scan_interval: 10
    headers:
        Content-Type: application/json
    method: POST
    payload: '{"did":"{{states.sensor.hot_tub_login_did.state}}","api_token":"{{states.sensor.hot_tub_login_api.state}}}"'
    sensor:
      - unique_id: cc2c04b2-4837-4599-b002-c682f504971a
        name: "hot_tub_status_all"
        value_template: "{{ value[:205] }}"

---OR---

  - resource: "https://MY-STATUS-URL/status"
    scan_interval: 10
    headers:
        Content-Type: application/json
    method: POST
   payload: '{{states.sensor.hot_tub_login_payload.state}}'
    
    sensor:
      - unique_id: cc2c04b2-4837-4599-b002-c682f504971a
        name: "hot_tub_status_all"
        value_template: "{{ value[:205] }}"

I either suspect the rest is running asynchronously and the previous request has not populated the state of the required values before this request is submitted and if this is the case what is the best way to manage it?

also if this is the case is suspect the scan_interval is also not working as it should work on the second update should it not?

OR

i have structured the payload incorrectly? and if so what is the correct structure?

so for anyone else that has this problem the “payload” element of a rest sensor will only accept a string.

However there was pull request to the main code base that adds an element called “payload_template” this will accept a sensor

my code now looks like

  - resource: "https://MY-STATUS-URL/status"
    headers:
      Content-Type: application/x-www-form-urlencoded
    method: POST
    payload_template: '{{ states("sensor.hot_tub_login_payload")|string }}'