Use of template in rest sensor - payload

I’m working on a rest sensor which does a POST request and requires a start and end date.
I read in the official docs that only a string is allowed for the payload.
is there no other way to use a template in there?

What I’m trying to achieve but doesn’t work unfortunately:

- platform: rest
  authentication: basic
  username: admin
  password: !secret mysecret
  resource_template: ""
  payload: >
      "startDate" : "2024-02-01",
      "endDate" : "{{ now().date() - timedelta(days=1) }}" 
  method: POST
  name: "test api"
  unique_id: test_api
  value_template: 'Active'
  scan_interval: 300

this way I’m able to retrieve data from yesterday automatically, otherwise I have to change the endDate everything in the rest sensor.


What does the response look like without dates? Can you create a template sensor from that sensor that extracts what you wish? I use a rest sensor payload like this:

payload: '{"KEYLIST": [{"CODESET": {{ codeset | int }},"CODE": {{ code | int }},"ACTION":"KEYPRESS"}]}'

Watching your single/double quotes and putting it all in a string (of JSON).

I’ve answered my own question. it’s possible to use “payload_template” instead of “payload”!
But it’s not documented on the official docs yet.

It isn’t in an official release yet. It’s in 2024.6.

1 Like