Use of template in rest sensor - payload

Hi,
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: "https://mysite.com/v1/json_data"
  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.

Thanks

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

Does anyone know if “payload_template” ever made it into an official release as I can’t see it in the 2024.6 release notes?

OK, thanks. I’ve now implemented ‘payload_template’ in a POST command to a remote server but am having problems - I’m clearly missing something / making a simple mistake and would massively appreciate some assistance.

I have successfully used ‘payload_template’ with the following code (plus multiple other derivatives that all use a fixed text string like below):

payload_template: >-
{% set fr_text = '{ \"text\" : [\"' + 'A test string' + '\"], \"target_lang\" : \"EN\" }' %}
{{ fr_text }}  

… but when I substitute ‘A test string’ with the state value of a sensor the payload is no longer considered valid by the remote server.

payload_template: >-
{% set fr_text = '{ \"text\" : [\"' +  states('sensor.sourcetextsensor')  + '\"], \"target_lang\" : \"EN\" }' %}
{{ fr_text }}  

Unfortunately the remote server doesn’t provide any error response so I can’t easily debug. I have however tried using the above code (and multiple other versions) in ‘Developer Tools / Template’ and frustratingly they all appear to work fine and return exactly the same output as when using the static text.

Any ideas would be greatly appreciated.

Indent your template in relation to the yaml field it’s in.

Also you can just make a normal output and use | to_json

Apologies, that was just bad formatting when I copy / pasted the code in the post as it is already indented:

payload_template: >-
  {% set fr_text = '{ \"text\" : [\"' +  states('sensor.sourcetextsensor')  + '\"], \"target_lang\" : \"EN\" }' %}
  {{ fr_text }}

I did also try the to_json idea but again hit the same issue, here’s my code:

payload_template: >-      
  {% set fr_text = '{ \"text\" : [\"' +  states('sensor.sourcetextsensor')  + '\"], \"target_lang\" : \"EN\" }' %}
  stringified object: {{ fr_text }}
  object|to_json: {{ fr_text|to_json) }}   
payload_template: >
  {{ {'text': [states('sensor.sourcetextsensor')], 'target_lang': 'EN'} | to_json }}