Is it possible to create a toggle switch on the interface and have it run a script when toggled on and a different script when toggled off?

The specific scenario I want to use it is change when automations are in effect. When it is toggled to ‘on’ the EV CHARGE PRIORITY automations are enabled (they check load vs ev charge vs solar generation in real time) and when the toggle is off these EV CHARGE PRIORITY are disabled and similar automations but diverting and managing load vs battery charge vs solar generation.

The above is slightly simplified but in essence what I want to find out and have been so far unsuccessful is how to create a toggle switch on the interface that runs a script when toggled on and different script when toggled off.


Look at a template switch. Ie

  - platform: template
        value_template: "if priority automation is turned on"
          service: script.ev_priority
          service: script.ev_non_priority

Awesome, done that but cannot find the step to link the code to the GUI … do I create a switch card then link the code as an entity?

You would need to add this to your configuration.yaml and then restart HA. Once you have template sensors, you can just reload them when you make changes via Developer Tools - Yaml tab.

Ok am making some progress. Didn’t realise the YAML config needed a restart - looks like in most instances a quick reload also does the trick but not confident that everything gets refreshed with a quick reload so when things don’t work am restarting the system and double checking.

So, this is where I am at. The switch is created and can see it as an entity with this code. The 2 test scripts are empty to ensure they are not causing any of the issues I am seeing.


  • platform: template
    unique_id: asdf
    value_template: “{{ is_state(‘switch.ev_mode’, ‘on’) }}”
    service: script.test1
    service: script.test2

1/ When I go to dev tools to set the state I can see in the log that the state is being set without triggering the scripts. I’m assuming this is ok.
2/ When I click the switch through any other way (via entities in the settings / adding it as a button / previewing the entity whilst adding it a card - in the LOG I see that it has triggered the switch but then run the 2 states. What am I doing wrong?

test2 turned off triggered by service Switch: Turn off
10:30:59 AM - 1 minute ago - Adrian
test2 turned on triggered by service Switch: Turn off
10:30:59 AM - 1 minute ago - Adrian
test2 started triggered by service Switch: Turn off
10:30:59 AM - 1 minute ago - Adrian - Traces

Not tested but think it is caused by your value_template. Not sure it should reference itself. If you dont have an entity that shows which mode is enabled, then maybe create a toffle helper, in you turn on and off sections, also turn this toggle on and off and use that toggle as the value template.

Also note, to properly format code when posting, enclose it in 3 ` chars at the start and end.

Sorry for stepping in, but the used format/version of the template switch is the old or legacy version and shouldn’t be used for newly configured template entities. :wink: :slight_smile:

This is nothing very important, the legacy format will still be supported for a long time, but the new format offers trigger based templates, and that would fit in here perfectly. :slight_smile:

But I’d like to offer another way, that seems a little easier, especially for a new user: two automations! :slight_smile:

The first one triggers from on to off, and the other one from off to on. That would work with not much hassle, and you would need just one input helper.

But the fancier and cooler solution is the template based trigger. :laughing:

This is cool and all, but I’d just create an input boolean and throw it on the dashboard as a switch or a button.

Then I’d write two automations using the state change of the boolean to do whatever I wanted done.

Call me simple, but it works; I’ve seen me do it.

You link to

Trigger-based template binary sensors, buttons, images, numbers, selects and sensors

but do you have an example of a trigger based switch ? A switch is not listed ‘Configuration variables’ in that section.