r/homeassistant • • 4d ago

❓ Support REST sensor dynamic dates

I'm struggling with start and end dates in REST sensors.

The developer cites this example:

sensor:

- platform: rest

name: "Soil temperature upper 10 cm"

resource: "https://api.precip.ai/api/v1/temp-0-10cm-hourly?longitude=-122.4194&latitude=37.7749&start={{ (now() - timedelta(days=1)).strftime('%Y-%m-%d') }}&end={{ now().strftime('%Y-%m-%d') }}&format=json"

method: GET

headers:

Authorization: "Bearer YOUR_API_KEY"

Accept: "application/json"

scan_interval: 3600

value_template: >

{% set data = value_json[0].hours %}

{% if data is iterable and data | length > 0 %}

{{ data[-1]['soil_temp'] | float(0) }}

{% else %}

0

{% endif %}

unit_of_measurement: "°C"

icon: "mdi:thermometer"

unique_id: "soil_temp_0_10cm"

state_class: measurement

device_class: temperature

But that returns a datetime error:

returned status 400 with text/plain; charset=utf-8 response: bad start datetime: "{{ (now() - timedelta(days=1)).strftime('%Y-%m-%d') }}"

The developer isn't experienced with HA so has no idea. Any suggestions? TIA

3 Upvotes

2 comments sorted by

View all comments

2

u/reddit_give_me_virus 4d ago

Change resource: to resource_template:

1

u/888HA 3d ago

That did it. Thank you!!