Let my HA make HTTP POST requests with dynamic payload

I’m looking for a way to collect data from sensors on remote rest web server. But can’t find a way to include dynamic sensor data into request payload.

I tried to create a rest sensor that sends info to remote server with post and receives a target temperature. It works exept payload is static and includes sensor’s name, not value.

sensor:
  - platform: rest
    name: web_sensor
    resource: http://host:port/url
    method: POST
    headers:
      Content-Type: application/json
    payload: >
      {
        "current_temp": "{{ states('sensor.temperature_sensor')|float }} "
      }
    json_attributes:
      - target_temp
    value_template: 'OK'

On remote server’s side request body is:

{"current_temp": "{{ states('sensor.temperature_sensor')|float }}"}

Please, guys, show me the way I can make classic REST POST requests with dynamil payload.
Tnanks.

Bump. Still cannot make it work.

Kind of unusual (at least for HA) using one entity to both send and receive data.

I’d suggest using two – use a RESTful Command to send the data (and you can have it sent periodically, if necessary, using an automation), and use the RESTful sensor just for receiving data (in this case target temperature.)

2 Likes

RESTful Command - haven’t seen it. Thanks!

1 Like