RESTful sensor: please allow templates for the Post payload

Continuing the discussion from RESTful sensor — template for the post payload?:

Right now, the payload for sensors using the REST platform and http POST instead of GET is just a fixed string. This can work in simple situations, but in many cases, that needs to vary. For example, I want to use the RCC-ACIS API to get historical weather data, and that needs the date in the payload. There are many other APIs that work this way.

Please consider adding a payload_template option.

Thank you! (See also https://github.com/home-assistant/core/issues/30786, where someone else makes a similar request.)

1 Like

@dev0 Would you be open to process the feedback and reopen the PR?
Or let somebody else take your changes and open a new PR (with reference to yours) to land this very helpfull change?

2 Likes

It seems that the developers ignore our requests and problems.

I need to change destination temperature of my heating device via payload with variable value.
But I cannot do this because the payload does not allow the use of template values in its body.
For example, my payload seems like following:
{"action":"setEnvGoal","login":"<login>","key":"<key>","deviceId":<dev_id>,"objId":<obj_id>,"goal":<degrees>}
I need to template: <login>, <key>, <dev_id>, <obj_id>, and <degrees>
But OK, I’m ready to replace most of these parameters with constants, but except of <degrees>.
Please add “payload_template” functionality to this integration.

This is an open source project — people work on the areas they are interested in. It doesn’t really do any good to complain that their interests and priorities are not yours or mine.

Glad I found this thread before spending another hour wondering why my payload template wasn’t working :frowning:

1 Like

well any updates?? as i see its closed…

I also need to use variables in the payload. Any updates?

payload_template pull request should definitely be reopened and merged in the core code. Home Assistant has great integration options, however the inability to customize payload is a strong shortcoming.

2 Likes

Hello, what’s the status? I’m making my transition to Home Assistant and I’m trying to integrate my alarm but I’m stuck because I can’t use templates in the payload… It’s pretty frustrating to have spent days working on my Home Assistant configuration and to find myself stuck because of this simple problem that seems to have a possible fix.

1 Like

A certain somebody would be our hero if he re-opened this pull request and finished it off…

Hello folks. My first post here :slight_smile:

I’ve been using HASS for barely 3 days, and needed this feature desperately to talk to a GraphQL endpoint from my own sensors project, so decided to setup the dev env and give it a go.

As this code is all new for me I’d need a couple weeks to find my bearings first, but I do have payload_template working locally. I’ve taken the original PR, and added changes asked for in review, plus dynamic refresh via the co-ordinator as I needed that too. I’ll put up a new PR once I get my head around the process / expectations.

This is the config I have tested with:

- scan_interval: 60
  resource: https://localhost/api
  method: POST
  verify_ssl: true
  headers:
    Content-Type: application/json
    User-Agent: Home Assistant
  payload_template: >
    {% set fromDate = (now() - timedelta(minutes=1)).strftime('%Y-%m-%dT%H:%M:00Z') %}
    {% set payload = {"query":"query SensorQuery($fromDate: DateTime!) {data: search(sensor: BME280, fromDate: $fromDate, sortOrder: desc, limit: 1) {temp_c humidity pressure}}","variables":{"fromDate": fromDate}} %}
    {{ payload | to_json }}
  sensor:
    - unique_id: garage_internal_temp_c
      name: "Garage Internal: Temperature"
      force_update: true
      device_class: "temperature"
      unit_of_measurement: "°C"
      value_template: "{{ value_json.data.data[0].temp_c }}"
    - unique_id: garage_internal_humidity
      name: "Garage Internal: Humidity"
      force_update: true
      device_class: "humidity"
      unit_of_measurement: "%"
      value_template: "{{ value_json.data.data[0].humidity }}"
    - unique_id: garage_internal_pressure
      name: "Garage Internal: Pressure"
      force_update: true
      device_class: "pressure"
      unit_of_measurement: "hPa"
      value_template: "{{ value_json.data.data[0].pressure }}"

And this is what it looks like in the logs:

2024-01-07 05:01:05.638 DEBUG (MainThread) [homeassistant.components.rest.data] Updating from https:/localhost/api
2024-01-07 05:01:05.704 DEBUG (MainThread) [homeassistant.components.rest] Finished fetching rest data data in 0.067 seconds (success: True)
2024-01-07 05:01:05.704 DEBUG (MainThread) [homeassistant.components.rest.data] Data fetched from resource: {"data":{"data":[{"temp_c":9.29296875,"humidity":58.78240717052532,"pressure":1020.7914838366156}]}}
2024-01-07 05:01:05.704 DEBUG (MainThread) [homeassistant.components.rest.data] Data fetched from resource: {"data":{"data":[{"temp_c":9.29296875,"humidity":58.78240717052532,"pressure":1020.7914838366156}]}}
2024-01-07 05:01:05.704 DEBUG (MainThread) [homeassistant.components.rest.data] Data fetched from resource: {"data":{"data":[{"temp_c":9.29296875,"humidity":58.78240717052532,"pressure":1020.7914838366156}]}}

and I can see correct values in /config/entities

2 Likes

My PR was merged into core, so hopefully will become available with the next release.

1 Like

Amazing! Thank you!

1 Like