What Is The Correct Syntax For Restful Switch

According to the documentation, I should be able to use one API URL to handle multiple switches (and sensors). I am able to get multiple sensors to work, but when I add the switches and check my configuration I get the following error. Invalid config for [rest]: [switch] is an invalid option for [rest]. Below is a snippet of the code: (Note: I am using an include file to handle all my rest api)

(configuration.yaml)

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
sensor: !include sensors.yaml
switch: !include switch.yaml
template: !include templates.yaml
mqtt: !include mqtt.yaml
#command_line: !include command_line.yaml
#modbus: !include modbus.yaml
rest: !include rest.yaml
rest_command: !include rest_command.yaml

(rest.yaml)

  - resource: http://10.100.100.225/state.xml
    scan_interval: 10    
    method: GET

    sensor:
      - name: barn_furnace_heater_x310_input1
        unique_id: 9ef03a74-d2e6-11ed-afa1-120420230001
        value_template: '{{ value_json["datavalues"].input1state }}'      
      - name: barn_furnace_room_x310_sensor1
        unique_id: 9ef03a74-d2e6-11ed-afa1-120320230101
        unit_of_measurement: "°F"                
        value_template: '{{ value_json["datavalues"].sensor1 }}'

    switch:
      - name: barn_furnace_heater_x310_relay1
        unique_id: 9ef03a74-d2e6-11ed-afa1-112420230001
        value_template: '{{ value_json["datavalues"].relay1state }}'
        turn_on:
          - service: rest_command.x310_post_api
            data:
              item: "relay1state=1"
        turn_off:
          - service: rest_command.x310_post_api
            data:
              item: "relay1state=0"

The documentation you linked to is for restful sensors, not switches. Switches are documented here:

It is my understanding if you have an API returning multiple sensors in the same “post” you could use the restful platform instead of the rest platform. The documentation you listed does work. I was trying to avoid calling the same API repeatedly for different switches and sensors and use the restful platform. According to the example in the restful documentation it seems switches and sensors can both be used. If I am misunderstanding this I would greatly appreciate somebody explaining this to me.

That example is incorrect. The rest integration only supports sensors and binary sensors:

https://www.home-assistant.io/integrations/rest/#sensor

I have created a PR to get that example corrected.

1 Like

Thank you for the heads up, it’s much appreciated!

Do you know why the rest integration supports the sensor and binary_sensor platforms but not the switch platform? Looking at the code in components/rest, sensor.py and binary_sensor.py share common code (e.g. class RestData) to achieve this, but switch.py is fairly standalone. Similarly, the RESTful Switch integration is not as flexible as the RESTful Sensor and RESTful Binary Sensor integrations, e.g. RESTful Sensor and RESTful Binary Sensor support a template for ‘resource’ but RESTful Switch does not. Is this different treatment just a historical accident or is there a fundamental reason?

Possibly because when @bdraco wrote this new functionality it was to fix an issue with multiple rest sensors using the same endpoint.