Switch -> Run Script

Could anybody can help me in this:
I have 2 scripts:

  1. device_on - Power on device, some action
  2. device_off - Power off device, some action

I need to link that scripts with one switch.
switch:
switches:
some_switch:
turn_on:
service: script.device_on
turn_off:
service: script.device_off
I’m beginner and I’m sure that something is missing. Please help :slight_smile:

Read here

You need to use the template platform I think and provide a template to work out whether the state should be on or off.

1 Like

Thank you, my code should be like:
switch:
platform: template
value_template: "false"
switches:
some_switch:
turn_on:
service: script.device_on
turn_off:
service: script.device_off

Well, it’s not working like I need. Switch turns off after activating.
Can anyone tell me please what value_template I should use for that switch will not turning off?

What is the device and do you have anyway of sensing if it is off or on?

Other option is to use and input_boolean as the template value and get your tunr on and off scripts tonset that at the same time.

1 Like

This is an amplituner with no sensing of it is on or off

You can use an input_boolean and start the scripts with an automation.

input_boolean:
  amplituner:
    name: Amplituner
    inital: off

automation:
  turn_amplituner_on:
   trigger:
    platform: state
    enitity_id: amplituner
    from: off
    to: on
  action:
    service: script.turn_on
    entity_id: script.amplituner_on

Vice versa for turning off.

3 Likes

Thanks for help! In my case it should like to be:

input_boolean:
  amplituner:
    name: Amplituner
    inital: off

automation:
  - alias: amplituner_on
    trigger:
      platform: state
      entity_id: input_boolean.amplituner
      from: 'off'
      to: 'on'
    action:
      service: script.turn_on
      entity_id: script.device_off

My overall goal is to have alexa run a tv script for on and one for off.

I’m having trouble with the formatting of the input boolean component. Am I correct in placing it in with the automations?
Below I have one of the automations that I already have working, I can’t get past the input boolean parsing block errors to add the new automation that I want to put in place for the script. I am using the code from the previous post just to get the formatting in order.

  - alias:  Morning Lights On
    trigger:
      platform: time
      at: "05:00:00"
    action:
      service: switch.turn_on
      entity_id: group.night_lights

          input_boolean:
             amplituner:
             name: Amplituner
             inital: off

          - alias: amplituner_on
            trigger:
                platform: state
                entity_id: input_boolean.amplituner
                from: 'off'
                to: 'on'
            action:
                service: script.turn_on
                entity_id: script.device_off

It needs to go in it’s own file if you use separate files but with out the input_boolean: or in the configuration.yaml like you have it there, i.e. outside of the automation.

Ok, so I need to create a boolean yaml and its state will be referenced by the alias I create for each script?

Should look like this…

if in configuration.yaml

input_boolean:
  amplituner:
    name: Amplituner
    inital: off

automation:
    - alias:  Morning Lights On
        trigger:
          platform: time
          at: "05:00:00"
        action:
          service: switch.turn_on
          entity_id: group.night_lights

automation 2:
    - alias: amplituner_on
      trigger:
        platform: state
        entity_id: input_boolean.amplituner
        from: 'off'
        to: 'on'
      action:
          service: script.turn_on
          entity_id: script.device_off

You will have to modify for separate files - see here…

I reread and split up my configs and still could not get it to work…until…I realized that “initial” was misspelled and that is why I was not loading. Still not done making this work, but man that wasted some time…:scream: Thanks again for the help though, the end is in sight.

Update: Got it working. Thanks again.

I missed the initial miss-spelling too :frowning: , but glad you got it working :slight_smile: