Why does this REST template sensor not setup correctly?

Why does this sensor fail at HA Start but if I then reload REST entities from the Dev tools it works?

I appreciate that you won’t be able to directly test this without an api code but I’m hoping that won’t be necessary.

Thanks.

sensor:
  - platform: rest
    name: tomorrow_io_data
    unique_id: tomorrow_io_data
    resource_template: >
      {% set latitude = state_attr('zone.home', 'latitude') %}
      {% set longitude = state_attr('zone.home', 'longitude') %}
      {% set fields = [
        "temperature",
        "temperatureApparent",
        "dewPoint",
        "windSpeed",
        "windDirection",
        "windGust",
        "humidity",
        "precipitationIntensity",
        "rainIntensity",
        "freezingRainIntensity",
        "snowIntensity",
        "sleetIntensity",
        "precipitationProbability",
        "precipitationType",
        "rainAccumulation",
        "snowAccumulation",
        "visibility",
        "cloudCover",
        "cloudBase",
        "cloudCeiling",
        "uvIndex",
        "uvHealthConcern",
        "evapotranspiration",
        "weatherCode",
        "weatherCodeDay",
        "weatherCodeNight"
      ] %}
      {% set timesteps = ["current", "1h", "1d"] %}
      {% set start_time = states('sensor.date_time_iso') %}
      {% set end_time = (as_datetime(start_time) + timedelta(days = 1)) | replace(' ', 'T') %}
      {% set get_url = 'https://api.tomorrow.io/v4/timelines?location=' ~ 
                        latitude ~ ',' ~
                        longitude ~
                        '&fields=' ~ fields | join(',') ~
                        '&timesteps=' ~ timesteps | join(',') ~
                        '&startTime=' ~ start_time ~
                        '&endTime=' ~ end_time ~
                        '&timezone=Europe/London' ~
                        '&units=metric' ~
                        '&apikey=xxxxx' %}
      {{ get_url }}
    value_template: >
      Timestamp: {{ now() }}
    json_attributes_path: $.data
    json_attributes:
      - timelines
    scan_interval:
      minutes: 15

image

After reload…

Based on the error message, the value of sensor.date_time_iso is none when the template is evaluated on startup. That entity is based on the Time & Date integration and my guess is it has no value because the REST integration is loaded before the Time & Date integration.

Replace this:

{% set start_time = states('sensor.date_time_iso') %}
{% set end_time = (as_datetime(start_time) + timedelta(days = 1)) | replace(' ', 'T') %}

with this:

{% set start_time = now().isoformat()[:19] %}
{% set end_time = (now() + timedelta(days = 1)).isoformat()[:19] %}

Yes, that was it. Thanks. (Twice in two days)

It did however lead to a separate problem in that the attributes do not get populated. I did wonder if it was because the response for the api was not fast enough but the default 10 seconds timeout seems like it should be plenty long enough (but I also tried making it 30 seconds)

Unless you have any other suggestions I guess I’ll just need to add an automation to run after HA has started to update the entity.

You’re welcome!

Please consider marking my post above with the Solution tag in order to help other users find answers to similar problems.

Done.
I was rather cheekily delaying in the hope that a plausible explanation for the lack of attribute population at start-up might appear!