Toggling switch with REST API is not working. Any ideas?

My indentation at the value_template line is different but here is my working template switch.

  - platform: template
    switches:
      lock:
        value_template: "{% if is_state('binary_sensor.lockstatus', 'on') %}on{% else %}off{% endif %}"
        turn_on:
          service: switch.turn_on
          entity_id: switch.lockaction
        turn_off:
          service: switch.turn_off
          entity_id: switch.lockaction

That for formatted wrong in the post, in reality it’s as yours.

But the turn_on and turn_off had wrong spacing, this still does not work, hmm…

- platform: template
  switches:
    test:
      value_template: "{{ is_state('light.kitchen_lamp', 'on') }}"
      turn_on:
        service: homeassistant.turn_on
        entity_id: switch.coffee_switch
      turn_off:
        service: homeassistant.turn_off
        entity_id: switch.coffee_switch

The switch.test is updated to “on” and “off” when I turn on/off the light, but it doesn’t turn on my coffee switch. What version of HA do you use @RobDYI?

I read your post again and I think the template switch is working as it should. The value_template doesn’t turn the switch on/off but only changes the state. In your case, the state of switch.test is light.kitchen_lamp but changing the state of light.kitchen_lamp shouldn’t turn switch.test on or off.

I’ve tested several times, the switch.test turns on as I turn on my kitchen light. And it turns off when I turn it off.
It makes sense, since the switch should be set to on/off depending on the light.
But it does not solves the mystery about not triggering the switch.coffe_switch.

What version of HA did you have?

Edit: When I mean turn off/turn on the switch.test I mean it sets the state of switch.test to “on” or “off”

If you think of the example of the skylight and someone opens the skylight manually, do you want the the skylight switch to start the motor that tries to open an already open skylight or do you want HA only to show the status of the skylight as open?

In your example, the kitchen light should not turn on the coffee machine, it should only show the test switch as on.

I’m with you, but the example is really bad. Further down it says:

This example shows a switch that takes its state from a sensor, and uses two momentary switches to control a device.

switch:
  - platform: template
    switches:
      skylight:
        friendly_name: 'Skylight'
        value_template: "{{ is_state('sensor.skylight.state', 'on') }}"
        turn_on:
          service: switch.turn_on
          entity_id: switch.skylight_open
        turn_off:
          service: switch.turn_on
          entity_id: switch.skylight_close

Reading the text, you get the impressions that the sensor.skyllight triggers the switches and control the device.

It also says

For example, if you have a garage door with a toggle switch that operates the motor and a sensor that allows you know whether the door is open or closed, you can combine these into a switch that knows whether the garage door is open or closed.

Which I interpret that they make a switch displaying the state of garage door instead of the sensor? It contradicts the example above.

English is not my native language, so I maybe mixing up things. But the template switch page is confusing…

What is more confusing is this:

turn_on (Required): Defines an action to run when the switch is turned on.
turn_off (Required): Defines an action to run when the switch is turned off.

It cleary states that it runs an action when the switch is on or off, wich supports the quote above about controling a device.

I agree with you guys, those instructions are far from clear.

I’m trying to implement the same with rest switch (https://home-assistant.io/components/switch.rest/). I’ll report here if I can get the same thing working with that one.

Seems that also the rest switch is not without problems as it seems to be sending some non-json data to API with POST as default. Trying to get past that somehow.

I did not get the platform: template to work but I ended up using platform: rest.

The use case is still the same: I will try to use this to poll I2C GPIO extension board pin statuses. This should function as the gateway to input switch state via API:

This is the result that actually works on that side:

switch:
  - platform: rest
    name: I2C_PIN_1
    resource: http://localhost:8000/i2c/ground_floor/1
    body_on: '{"active": "true"}'
    body_off: '{"active": "false"}'
    is_on_template: '{{value_json == {"active": "true"}}}'

The is_on_template can also be used to check related variables on HASS side, but I haven’t tested that yet. So not sure if and how it will work.

Please note that I had to use the template check to string value: value_json == {“active”: “true”}. I ended up with a problem when I set the json as boolean {“active”: true} it ended up showing True (with capital T) on the web server. I quess this caused it not to match on HASS side. Not 100% sure about it, but I did not feel like debugging this thing as I got it working.