How to make a switch on/off from script

hi
i have a script that i want to be as on/off button - so i can call alexa “turn on reciver” ans so on
i found that:

computer_on:
  sequence:
- delay: '00:00:01'
- service: switch.turn_on
  data:
    entity_id: switch.tv, switch.reciver, switch.computer

computer_off:
  sequence:
- delay: '00:00:01'
- service: switch.turn_off
  data:
    entity_id: switch.tv, switch.reciver

then i made switch

- platform: template
      switches:
        computer:
          value_template: "{{ is_state('script.computer_on', 'on') }}"
          turn_on:
            service: script.turn_on
            entity_id: script.computer_on
          turn_off:
            service: script.turn_on
            entity_id: script.computer_off

and it’s working but every time i press it (on or off) it getting back to off in the frontend

why? and is it the best way to do that?
thank you

Check Config did not show any error?
I just did some of my light switches to expose them to Alexa but the ones I have in my scripts.yaml are indented differently:

kitchen_light_on:
  sequence:
    - alias: Kitchen Light On
      service: switch.turn_on
      data:
        entity_id: switch.kitchen_ceiling_light

my understanding is

hass can send command on/off but if hass does not get a command back it goes back to what it was

A script is ‘on’ as long as it’s running and then goes to off.
You should group your switches and use the state of the group for the value_template.
value_template: "{{ is_state('group.your_switches', 'on') }}"

I have the exact same problem. Not sure what you mean by grouping the switches, since there is only one switch???

I tried adding an input_boolean and used that to set the state of the template switch. That worked insofar as the template switch remained in the on state, but then the scripts would not run, neither on nor off.

That’s because @Asaf_Davidof has multiple switches in his script.
If you have only one switch in the script, then
value_template: "{{ is_state('switch.your switch', 'on') }}"
should work.

Got it.

This is what I have:

switch:
  - platform: template
    switches:
      stereo:
        friendly_name: Panasonic CD
        value_template: "{{ is_state('switch.stereo', 'on') }}"
        turn_on:
          service: remote.send_command
          data_template:
            entity_id: remote.panasonic_cd
            command: "Aux"
        turn_off:
          service: remote.send_command
          data_template:
            entity_id: remote.panasonic_cd
            command: "Power"

When I turn on the switch, the turn_on command is executed, but then the switch turns right off again.

If I use an input_boolean to set the state like this:

input_boolean:
  stereo:
    name: "Stereo"
    initial: off

switch:
  - platform: template
    switches:
      stereo:
        friendly_name: Panasonic CD
        value_template: "{{ is_state('input_boolean.stereo', 'on') }}"
        turn_on:
          service: remote.send_command
          data_template:
            entity_id: remote.panasonic_cd
            command: "Aux"
        turn_off:
          service: remote.send_command
          data_template:
            entity_id: remote.panasonic_cd
            command: "Power"

then the template switch follows the input_boolean state and stays on, but the on and off commands are not executed.

Really stumped. Greatful for all help!

2 Likes

Where do you set the input_boolean?
This would work if you create two scripts.

script:
  stereo_turn_on:
    sequence:
      - service: remote.send_command
        data_template:
          entity_id: remote.panasonic_cd
          command: "Aux"
      - service: input_boolean.turn_on
        data:
          entity_id: input_boolean.stereo

  stereo_turn_off:
    sequence:
      - service: remote.send_command
        data_template:
          entity_id: remote.panasonic_cd
          command: "Power"
      - service: input_boolean.turn_off
        data:
          entity_id: input_boolean.stereo

And call these in your template switch

switch:
  - platform: template
    switches:
      stereo:
        friendly_name: Panasonic CD
        value_template: "{{ is_state('input_boolean.stereo', 'on') }}"
        turn_on:
          service: script.turn_on
          entity_id: script.stereo_turn_on
        turn_off:
          service: script.turn_on
          entity_id: script.stereo_turn_off
1 Like

Thanks a lot, but this made no difference. The scripts are only run when the template switch is set manually, not when it is set by the input_boolean…

Just to explain what I am trying to do: I want to use Alexa to turn on/off the stereo. Thus I need a single switch or an input_boolean that Alexa can set and that will run the remote commands.

The scripts work fine when run manually. Next I will try to run the scripts as automations directly off the input.boolean state. If that doesn’t work I am out of ideas.

Finally success!

Here is what I did:
configuration:

input_boolean:
  stereo:
    name: "Stereo"
    initial: off

automations:

  - alias: panasonic_on
    trigger:
      platform: state
      entity_id: input_boolean.stereo
      to: 'on'
    action:
      service: script.turn_on
      entity_id: script.cd_on
  - alias: panasonic_off
    trigger:
      platform: state
      entity_id: input_boolean.stereo
      to: 'off'
    action:
      service: script.turn_on
      entity_id: script.cd_off

and the scripts:

  cd_on:
    sequence:
      - service: remote.send_command
        data_template:
          entity_id: remote.panasonic_cd
          command: "Aux"
  cd_off:
    sequence:
      - service: remote.send_command
        data_template:
          entity_id: remote.panasonic_cd
          command: "Power"

Seems like there should be a simpler solution. Still don’t understand why the template switch didn’t work. Oh well, at least it works now. Thanks for the support!

1 Like

I had the excact same problem with alexa, and devices that doesn`t report back states.

The problem you now will encounter, is that if you turn off your your stereo from your remote, the state of the input_boolean will still be on, so when you ask alexa then, it will not excecute your on-script.
What you want is a template switch that excecutes your command, regardless of the state of input boolean (template switches can be on, but still excecute the on-scripts).

I saw you were close earlier, just adjust the template to if is_state like this:

 stereo:
   value_template: "{% if is_state('input_boolean.stereo', 'on') %}on{% else %}off{% endif %}"
   turn_on:
     service: remote.send_command
     data_template:
       entity_id: remote.panasonic_cd
       command: "Aux"
   turn_off:
     service: remote.send_command
     data_template:
       entity_id: remote.panasonic_cd
       command: "Power"

And that’s it :slight_smile:
Should work like that, you don`t need any more automations or anything.

Thanks a lot but we’re not quite there yet. This doesn’t work either. It is just the same as my first attempt: the template switch tracks the input_boolean but the commands are not executed. If I set the template switch manually, the on command is executed but the switch turns right back off.

Guess I’ll have to spend some more time understanding templating. It is still a mystery to me.

Good catch on the risk for state mismatch if someone uses the physical remote though. Maybe I’ll just hide that…

Ok, could you try using the scripts as on/off actions instead? That should work.
This works for me:

 bord:
   value_template: "{% if is_state('input_boolean.bord_dummy', 'on') %}on{% else %}off{% endif %}"
   turn_on:
     service: script.turn_on
     entity_id: script.bord_on
   turn_off:
     service: script.turn_on
     entity_id: script.bord_off

   bord_on:
     sequence:
     - service: remote.send_command
       data_template:
         entity_id: remote.stue
         command: PowerOn
         device: 45270305
         
   bord_off:
     sequence:
     - service: remote.send_command
       data_template:
         entity_id: remote.stue
         command: PowerOff
         device: 45270305

The template switch will not change it’s state, but should work as intended with the commands.

Just be sure to remove your automations, and be sure that it’s only the template switch you expose to alexa, not the input_boolean or scripts.

Yes! Using the scripts work, even though the template switch stays on all the time. It also requires that the input_boolean is set to initially: on and then never touched. Thank you so much for your help and Happy Easter!

Glad you got it working :slight_smile: I do not have any initial state on my input_boolean which also works, but since it`s never touched, that is irrelevant. Happy Easter!

I have the same problem as the topic starter. Created a switch which uses an on/off script but somehow the when I turn on the switch it goes back to off after 2 seconds.
I tried various combination with input_boolean or directly calling the remote send service but all does same. Any idea why?

Currently I have this:

configuration.yaml:

input_boolean:
  aircopower:
    name: Airco Power
    initial: off

scripts.yaml:

airco_on:
  alias: Airco Power On
  sequence:
  - service: remote.send_command
    entity_id: remote.xiaomi_miio_192_168_2_70
    data:
      command: 
      - 'm01mcwlk3m03mEsms5mEsmc0mgBHgEfLJiAR4EdzECPwCPA18FnwCFmoCPgWeCh4EfzCWTYIopnMwDSm0yCYYAg5iAR4DUgR+ARYI7ga9NQGjnABCzMAhAI/AjoEdJsA14DTg++BH4VWzMOAwCCm80G9IAg5hAA='
  icon: hassio:fan
  
airco_off:
  alias: Airco Power Off
  sequence:
  - service: remote.send_command
    entity_id: remote.xiaomi_miio_192_168_2_70
    data:
      command: 
      - 'm01mcwlk3m03mEsm0ymEsmc0mABHgEfLJjNJiAi4BEARvM5wBG4FkAa+AjAKFAEfMZqAj4FizUGJwCIBH8CzwBCAj8AiwHimIKDgEKBZ4DTTYPvwUPAR8KGwdJBHQBrwEfBQ8CPgCECGabzQb0gCDmEA'
  icon: hassio:fan

switches.yaml:

  - platform : template
    switches:
      airco_power:
        value_template: "{{ is_state('input_boolean.aircopower', 'on') }}"
        turn_on:
          service: script.airco_on
        turn_off:
          service: script.airco_off

So, the on/off script works and so the switch does but the switch state goes automatically to off again after 2 seconds (without sending the off command).
If I do it manually to off within those 2 seconds the off command is sent.

Why doesn’t the switch stay on?

i had the same problem - the state is staying off because you don’t set it to “on”

you need something like this in your script

 - service: input_boolean.turn_on
    target:
      entity_id: input_boolean.aircopower

and similary for your turn off script

Tried that as well but the switch returned to off again.

Hi,

can you give me a hint please, how to call a script entity by addressing a switch. or light. entity?
Broadlink sends IR commands by scripts, but script entities are not compatible with all the cards.

thanks

H

check the script name in developer tools