Simple Switch on/off using scripts

Hello guys!

Sorry to bother you, I need to create a couple of simple switches with on an off commands using scripts.

I’ve search for hours and decided to ask for help here:

Should I use template switches? How can I set it to start on OFF (if HA restarts) and then keep the switch locked when toggled on and off on their respective position?

I’ve tried this:

switch:

  • platform: template
    switches:
    alarme_casa:
    value_template: “{{ is_state(‘switch.alarme_casa’, ‘on’) }}”
    friendly_name: “Alarme da casa”
    turn_on:
    service: script.alarme_on
    turn_off:
    service: script.alarme_off

Can somebody help me?

Thanks!

A template switch would only be used if you were using some other entity to determine the state.
The easiest way to do what you want is to use an input_boolean.
Then create an automation based on that.
Here’s an example:

input_boolean
  run_sprinklers_now:
    name: Run Sprinklers Now
    initial: off
automation:    
- alias: autoSprinklers
  trigger:
    - platform: state
      entity_id: input_boolean.run_sprinklers_now
      to: 'on'
  action:
    - service: shell_command.rsprinklers
1 Like

Thanks @treno!

I’ve managed to create a switch with broadlink’s platform and changed the icon and assumed state using customization.

Thanks!!!