mattdm
(Matthew Miller)
February 7, 2022, 2:14pm
1
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.)
sfstar
(sfstar)
October 8, 2022, 11:55am
3
@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.
mattdm
(Matthew Miller)
November 18, 2022, 7:37pm
5
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.
pjn77
February 26, 2023, 8:14pm
6
Glad I found this thread before spending another hour wondering why my payload template wasn’t working
1 Like
malosaa
(Hector)
July 13, 2023, 6:56pm
7
well any updates?? as i see its closed…
I also need to use variables in the payload. Any updates?
Prcek
November 2, 2023, 9:47am
9
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.
home-assistant:dev
← DanielBaulig:restful_payload_template
opened 06:01AM - 06 Jun 22 UTC
<!--
You are amazing! Thanks for contributing to our project!
Please, DO N… OT DELETE ANY TEXT from this template! (unless instructed).
-->
## Proposed change
<!--
Describe the big picture of your changes here to communicate to the
maintainers why we should accept this pull request. If it fixes a bug
or resolves a feature request, be sure to link to that issue in the
additional information section.
-->
Adds a payload_template option to RESTful sensors to allow for dynamic rendering of the payload included in RESTful requests. Feature was requested several times, e.g. https://community.home-assistant.io/t/restful-sensor-please-allow-templates-for-the-post-payload/389843
## Type of change
<!--
What type of change does your PR introduce to Home Assistant?
NOTE: Please, check only 1! box!
If your PR requires multiple boxes to be checked, you'll most likely need to
split it into multiple PRs. This makes things easier and faster to code review.
-->
- [ ] Dependency upgrade
- [ ] Bugfix (non-breaking change which fixes an issue)
- [ ] New integration (thank you!)
- [x] New feature (which adds functionality to an existing integration)
- [ ] Breaking change (fix/feature causing existing functionality to break)
- [ ] Code quality improvements to existing code or addition of tests
## Additional information
<!--
Details are important, and help maintainers processing your PR.
Please be sure to fill out additional details, if applicable.
-->
Pretty straight forward change. Changed the config schema of RESTful to make `CONF_PAYLOAD` exclusive with the new `CONF_PAYLOAD_TEMPLATE` `sv.template` configuration field and check in `create_rest_data_from_config` if it was set and if so, render it to replace the currently blank `payload` value. Pretty much mirrors how `resource_template` works.
Considered just changing `CONF_PAYLOAD` to sv.template, but decided against it because there was a non-trivial risk of breaking peoples RESTful sensors that had a payload including the `{{` or `{%` character sequences.
- This PR fixes or closes issue: resolves [feature request](https://community.home-assistant.io/t/restful-sensor-please-allow-templates-for-the-post-payload/389843)
- Link to documentation pull request: Not added yet. Will add documentation once I get a green-light on the pull request.
## Checklist
<!--
Put an `x` in the boxes that apply. You can also fill these out after
creating the PR. If you're unsure about any of them, don't hesitate to ask.
We're here to help! This is simply a reminder of what we are going to look
for before merging your code.
-->
- [x] The code change is tested and works locally.
- [ ] Local tests pass. **Your PR cannot be merged unless tests pass**
- [x] There is no commented out code in this PR.
- [x] I have followed the [development checklist][dev-checklist]
- [ ] The code has been formatted using Black (`black --fast homeassistant tests`)
- [ ] Tests have been added to verify that the new code works.
If user exposed functionality or configuration variables are added/changed:
- [ ] Documentation added/updated for [www.home-assistant.io][docs-repository]
If the code communicates with devices, web services, or third-party tools:
- [ ] The [manifest file][manifest-docs] has all fields filled out correctly.
Updated and included derived files by running: `python3 -m script.hassfest`.
- [ ] New or updated dependencies have been added to `requirements_all.txt`.
Updated by running `python3 -m script.gen_requirements_all`.
- [ ] For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
- [ ] Untested files have been added to `.coveragerc`.
The integration reached or maintains the following [Integration Quality Scale][quality-scale]:
<!--
The Integration Quality Scale scores an integration on the code quality
and user experience. Each level of the quality scale consists of a list
of requirements. We highly recommend getting your integration scored!
-->
- [ ] No score or internal
- [ ] 🥈 Silver
- [ ] 🥇 Gold
- [ ] 🏆 Platinum
<!--
This project is very active and we have a high turnover of pull requests.
Unfortunately, the number of incoming pull requests is higher than what our
reviewers can review and merge so there is a long backlog of pull requests
waiting for review. You can help here!
By reviewing another pull request, you will help raise the code quality of
that pull request and the final review will be faster. This way the general
pace of pull request reviews will go up and your wait time will go down.
When picking a pull request to review, try to choose one that hasn't yet
been reviewed.
Thanks for helping out!
-->
To help with the load of incoming pull requests:
- [ ] I have reviewed two other [open pull requests][prs] in this repository.
[prs]: https://github.com/home-assistant/core/pulls?q=is%3Aopen+is%3Apr+-author%3A%40me+-draft%3Atrue+-label%3Awaiting-for-upstream+sort%3Acreated-desc+review%3Anone+-status%3Afailure
<!--
Thank you for contributing <3
Below, some useful links you could explore:
-->
[dev-checklist]: https://developers.home-assistant.io/docs/en/development_checklist.html
[manifest-docs]: https://developers.home-assistant.io/docs/en/creating_integration_manifest.html
[quality-scale]: https://developers.home-assistant.io/docs/en/next/integration_quality_scale_index.html
[docs-repository]: https://github.com/home-assistant/home-assistant.io
2 Likes
Ben90
November 10, 2023, 3:20pm
11
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…
ptashek
(ptashek)
January 7, 2024, 5:15am
13
Hello folks. My first post here
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
ptashek
(ptashek)
April 22, 2024, 11:37am
14
My PR was merged into core, so hopefully will become available with the next release.
1 Like