Handling Restful response

I have a restful command and can’t work out how to handle the 201 response from the API. My setup is:

rest_command:
  cycle_state:
    url: 'http://127.0.0.1:5000/cycle'
    method: post
    headers:
      Authorization: !secret token
      Content-Type: 'application/json'
    payload: '{"name": "1", "length": 10, "status": true, "delay_enabled": false, "start_date": "2023-07-04T00:00:00Z"}'

switch:
  - platform: template
    switches:
      automation_switch:
        friendly_name: "Automation Start/Stop"
        value_template: "{{ is_state('input_boolean.automation_status', 'on') }}"
        turn_on:
          - service: input_boolean.turn_on
            data:
              entity_id: input_boolean.automation_status
          - service: rest_command.cycle_state
        turn_off:
          - service: input_boolean.turn_off
            data:
              entity_id: input_boolean.automation_status
          - service: rest_command.cycle_state

any advice or resources would be great

A rest_command ignores the reply from the endpoint.
What do you want to do with the 201?

@koying thanks for that, I’m trying to set a sensor entity with the returned value. i.e in this case the returned payload is:

{
  "id": "123",
  "message": "Cycle created"
}

I haven’t included my sensor setup as its a big mess of trying different things. as of now i have:

sensor:
  - platform: rest
    resource: http://127.0.0.1:5000/cycle
    method: POST
    headers:
      Authorization: !secret token
      Content-Type: 'application/json'
    name: id
    value_template: '{{ value_json.id }}'
    scan_interval: 10
    json_attributes:
      - id
      - message

Sorry i should add the above works, I forgot to include the payload. its not exacly what i wanted but i should be able to trigger the sensor based on a switch as apposed to the scan time

A solution could be to define a restful sensor (RESTful - Home Assistant) with an absurdly long scan_interval, then use the homeassistant.update_entity to “update” it when needed. This way, you’ll be able to capture the id.

NOTE: Be careful though that you cannot prevent HA to update the sensor once at HA startup, so that might be a stopper for you.

1 Like

good point on the intial sensor update. will need to do a little thinking