Template switch for rest command

I have some lights that do not have status information and can be turned on/off via a rest command. I would like to control this with a switch, but the config below does not work, it says “Script script.kitchen_lights_on already running”. Any ideas?

rest_command:
  kitchen_lights_off:
    url: "http://192.168.1.135/ansluta/0"
  kitchen_lights_50:
    url: "http://192.168.1.135/ansluta/50"
  kitchen_lights_100:
    url: "http://192.168.1.135/ansluta/100"

script:
  kitchen_lights_on:
    sequence:
      - service: rest_command.kitchen_lights_100
      - service: switch.turn_on
        data:
          entity_id: switch.kitchen_lights
  kitchen_lights_off:
    sequence:
      - service: rest_command.kitchen_lights_off
      - service: switch.turn_off
        data:
          entity_id: switch.kitchen_lights

switch:
  - platform: template
    switches:
      kitchen_lights:
        value_template: "{{ states('switch.kitchen_lights') }}"
        turn_on:
          service: script.kitchen_lights_on
        turn_off:
          service: script.kitchen_lights_off

Use an input_boolean to track the state of the lights.

  • The scripts for turning the lights on/off also set the input_boolean’s state.
  • The Template Switch uses the input_boolean’s state to represent its own state.
input_boolean:
  kitchen_lights_state:
    name: Kitchen Lights State

script:
  kitchen_lights_on:
    sequence:
      - service: rest_command.kitchen_lights_100
      - service: input_boolean.turn_on
        entity_id: input_boolean.kitchen_lights_state
  kitchen_lights_off:
    sequence:
      - service: rest_command.kitchen_lights_off
      - service: input_boolean.turn_off
        entity_id: input_boolean.kitchen_lights_state

switch:
  - platform: template
    switches:
      kitchen_lights:
        value_template: "{{ is_state('input_boolean.kitchen_lights_state', 'on') }}"
        turn_on:
          service: script.kitchen_lights_on
        turn_off:
          service: script.kitchen_lights_off
2 Likes