Switch with rest_command

Hi,
I’m trying to configure a switch with rest_command component and some sensors that I have already, but I can’t get it to work.
This is my configuration, and I have tested the rest_command component with the “developer tools” and it works correctly.

rest_command:
  airzone_on:
    url: http://localhost:3000/api/v1/hvac
    method: PUT
    payload: '{"systemID":1,"zoneID":{{ zoneid }},"on":{{ on }}}'
    content_type:  'application/json; charset=utf-8'

switch:
  - platform: template
    switches:
      blueface:
        friendly_name: "Blueface On/Off"
        value_template: "{{ is_state('sensor.blueface_status', 'Running') }}"
        turn_on:
          service: rest_command.airzone_on
          data:
            zoneid: 1
            on: 1
        turn_off:
          service: rest_command.airzone_on
          data:
            zoneid: 1
            on: 0

I use the “paidload” to have a more generic rest_command. With developer tools, call service: rest_command.airzone_on --> data, {“zoneid”:1,“on”:1}, all ok

But, if now with the switch definition, you add it to a card, when I activate the switch it gives me an error: "Failed to call service switch / turn_on. Keywords must be strings"

Can anyone give me some help?

Thks in advance.

on is a poor choice of parameter name. In YAML, on will be replaced with true. So, when you do this:

        turn_on:
          service: rest_command.airzone_on
          data:
            zoneid: 1
            on: 1

it’s the same as doing this:

        turn_on:
          service: rest_command.airzone_on
          data:
            zoneid: 1
            true: 1

And true is a boolean, not a string.

At the very least you need to change it to:

        turn_on:
          service: rest_command.airzone_on
          data:
            zoneid: 1
            'on': 1

But I would recommend changing on to something like turn_on:

rest_command:
  airzone_on:
    url: http://localhost:3000/api/v1/hvac
    method: PUT
    payload: '{"systemID":1,"zoneID":{{ zoneid }},"on":{{ turn_on }}}'
    content_type:  'application/json; charset=utf-8'

switch:
  - platform: template
    switches:
      blueface:
        friendly_name: "Blueface On/Off"
        value_template: "{{ is_state('sensor.blueface_status', 'Running') }}"
        turn_on:
          service: rest_command.airzone_on
          data:
            zoneid: 1
            turn_on: 1
        turn_off:
          service: rest_command.airzone_on
          data:
            zoneid: 1
            turn_on: 0

Hi pnbruckner,

Thank you very much for the answer. I have made the configuration that you have recommended and it works perfectly. Thanks for the advice.

1 Like