Executing REST requests sequentially, one after another

Hi all

I have a REST-configuration that receives water data from a website.

First you need to get a token for permission, and then get a client based on token. Then you can get data. To do this, Scan_Interval in each request is 10 seconds more.

When the system works in a standard configuration, the sensors receive information once a day on request sequentially.

But when rebooting the Homa assistant, all sensors are trying to fulfill requests at the same time. Since the token has not yet been received, subordinate requests have a mistake that will be corrected only every other day.

The question is, is it possible to pause so that the requests are executed one after another, and not at the same time? I would be grateful for any help.

The code is given below.

rest:
# Request to receive a token
  - resource: https://xxx.yy/login
    scan_interval: 86400
    method: POST
    headers:
      Content-Type: application/json;
      Connection: keep-alive;
    payload: !secret me_payload
    sensor:
      - name: "ME Token"
        unique_id: me_token
        icon: mdi:cloud-key-outline
        value_template: "{{ value_json.token }}"

# Client request
  - resource: https://xxx.yy/client
    scan_interval: 86410
    method: GET
    headers:
      Content-Type: application/json;
      X-Auth-Tenant-Token: "{{- states('sensor.me_token') -}}"
    sensor:
    - name: "ME Client"
      unique_id: me_client
      icon: mdi:home-account
      value_template: "{{ value_json['items'][0]['name'] }}"
      json_attributes_path: "$.items.[0]"
      json_attributes:
        - id
        - address

#Request for counter data
  - resource_template: "https://xxx.yy/meters/{{- state_attr('sensor.me_client','id') }}"
    scan_interval: 86420
    method: GET
    headers:
      Content-Type: application/json;
      X-Auth-Tenant-Token: "{{- states('sensor.me_token') -}}"
    sensor:
      - name: "ME ColdWater"
        unique_id: me_meter_coldwater
        icon: mdi:water-thermometer-outline
        value_template: "{{ value_json[0]['meter']['name'] }}"
        json_attributes_path: "$.[0]"
        json_attributes:
          - meter
      - name: "ME HotWater"
        unique_id: me_meter_hotwater
        icon: mdi:water-thermometer
        value_template: "{{ value_json[1]['meter']['name'] }}"
        json_attributes_path: "$.[1]"
        json_attributes:
          - meter

After searching the forum and documentation, I realized that there is no such thing as a pause or delay in REST. So the following solution is possible.
Set the scan_interval: parameter to a large value, for example:

 scan_interval: 31557600

Create the following automation:

## Prompt at system startup
alias: Get data on system startup
triggers:
  - trigger: homeassistant
    event: start
actions:
  - wait_template: "{{ has_value('sensor.me_token') }}"
  - action: homeassistant.update_entity
    metadata: {}
    data:
      entity_id:
        - sensor.me_client
  - delay:
      hours: 0
      minutes: 0
      seconds: 2
      milliseconds: 0
    enabled: true
  - action: homeassistant.update_entity
    metadata: {}
    data:
      entity_id:
        - sensor.me_meter_coldwater
        - sensor.me_meter_hotwater
mode: single

If you want to update sensors according to your conditions, replace the trigger

triggers:
  - trigger: homeassistant
    event: start

with the one you need.