How to use template values in REST sensor payload?

Please help me.
My service provider requires POST queries in json format.
Like this:
{"action":"getDevices","login":"mylogin","key":"myapikey"}
I wrote the following sensor:

  - platform: rest
    name: "mh_getdevices"
    resource: https://my2.myheat.net/api/request/
    method: POST
    timeout: 30
    scan_interval: 10
    json_attributes_path: "$.data"
    json_attributes:
      - devices
    headers:
      Content-type: "application/json"
      Accept: "text/plain"
      Content-Encoding: "utf-8"
    payload: '{"action":"getDevices","login":"mylogin","key":"myapikey"}'
    verify_ssl: false
    value_template: '{{ value_json.err == 0 }}'

It works perfectly, but I don’t want to use fixed values for “mylogin” and “myapikey”. Instead of these I want to use template values, for example {{ states.input_text.my_login.state) }} etc.
I have tried several options for embedding template value into payload string, but all attempts resulted in an error. For example:
payload: '{"action":"getDevices","login":{{states.input_text.my_login.state}},"key":"myapikey"}' (without quotes)
payload: '{"action":"getDevices","login":"{{states.input_text.my_login.state}}","key":"myapikey"}' (with quotes)
payload: '{"action":"getDevices","login":states.input_text.my_login.state,"key":"myapikey"}' (without braces)
payload: '{"action":"getDevices","login":' + {{states.input_text.my_login.state}} + ',"key":"myapikey"}' (concatenate with “+” sign)
…and many more options, but no result.

2 Likes

Hi. I am looking at the same issue. Did you find a solution?

Similar issue here: I simply don’t find a way to get the resource_template working.
Also customizing the input to be hidden seems to have no effect (homeassistant os)

input_text:
  secret_owm_apikey:
    initial: !secret owm_apikey
    mode: password

homeassistant:
  customize:
    input_text.secret_owm_apikey:
      hidden: true
resource_template: https://api.openweathermap.org/data/2.5/onecall?lat={{ state_attr("zone.home","latitude") }}&lon={{ state_attr("zone.home","longitude") }}&lang=de&units=metric&exclude=minutely&appid={{ states("input_text.secret_owm_apikey") }}

The expression evaluates fine with the template editor in the ui

Did you ever figure this out?

Templates are just not implemented in this component.

I started a fork with templates included but ended up using https://github.com/danieldotnl/ha-multiscrape instead, which supports templates wherever you might need it :slight_smile:

I just came across this trying to do the same thing, and your solution seems to work fine. I only added the input_text and didn’t try to hide the password in the ui (although i may). But adding the input text into the resource template worked fine. Maybe there’s been an update that added the functionality since you posted this, so just wanted to share in case anyone else is looking for this.

Please tell me how can I use template in payload with multiscrape integration?
For example, my payload (as constant string):
{"action":"getDeviceInfo","deviceId":"12345","login":"mylogin","key":"myapikey"}
But I want to use deviceId as template instead of “12345”. For example, as {{states(‘sensor.device_id’)}}

Use ‘payload_template’ and unleash the full power of templating :slight_smile:

“payload_template” is not allowed:

Logger: homeassistant.config
Source: config.py:835
First occurred: 12:19:45 (1 occurrences)
Last logged: 12:19:45
Invalid config for [multiscrape]: [payload_template] is an invalid option for [multiscrape]. Check: multiscrape->multiscrape->0->payload_template. (See ?, line ?). Please check the docs at https://github.com/danieldotnl/ha-multiscrape

Hmm, seems as if this has been removed from the component. I am not using HACS for this integration, so there is an older version running in my system.

I saw your open issue, let’s hope you get an answer soon!