Switch Template - Execute Automation

Hi All, I have tried to do something like this earlier and had a couple of people help but Im not quite their. I do not know how to right the code for this (newbie but getting better).
I have an automation that I would like to execute from a switch on my frontend. I currently am doing this succesfully with a button, (thanks to @Burningstone) however it is a switch that I really would prefer.
I assume that a switch template is the way to go but Im not skilled enough to produce this.

I have created a switch_template to turn automations on/off which was a learning curve but well worth it. However actually getting a switch to execute or ‘run’ the automation is what Im trying to achieve.

Hope someone has already done this and can post an example so I can read, copy, steel and learn from.

Thanks

Did you take a look at the docs here?

Automations (https://www.home-assistant.io/docs/automation/) run based on TRIGGERS. Think of it like this, if the automation is ON, that really just means it’s ACTIVE (ie. able to be TRIGGERED [started]).

The TRIGGER is really just a way to START the automation. Could be a specific TIME, could be when motion is sensed, could be when a switch is turned on/turned off/toggled, etc. There needs to be an ACTION in order to TRIGGER / START the automation.

I suggest you read more about them in the link above.

What you might be after is a script. When you script.turn_on you START/TRIGGER the script. The real difference b/w an automation and a script is an automation has to be TRIGGERED, whereas a script can just be TURNED_ON/started manually. Automations can there be set with ‘listeners’ (when this happens, start), vs. scripts that have to be ‘manually’ started. The nuance is somewhat subtle, but has huge implications.

I have a handful of template_switches that when I press them, they DO something - they call a script. Here’s one for my front driveway gate, for example. - when it’s turned on or off, it calls a script.

    front_drive_gate:
      friendly_name: "Front Driveway Gate"
      value_template: "{{ is_state('binary_sensor.front_gate_contact', 'on') }}"
      turn_on:
        service: script.turn_on
        data:
          entity_id: script.front_gate_toggle
      turn_off:
        service: script.turn_on
        data:
          entity_id: script.front_gate_toggle
      icon_template: "mdi:gate"

Anyhow, best bet is to get an understanding of the building blocks that HA uses (sensors, switches, etc.) and then move onto how you can have them interact (scripts/automations). This will give you a firm foundation to work from, and then you can move on to input_*, groups, etc. that can make your system even more efficient (relative term :joy:) and powerful.

3 Likes

cheers. I will start scripting and see where I end up.

So Gentleman, success is mine. This may not be pretty but for a first time script and triggering an automation with a switch it works.
For future readers (and better script writers).
This is what I put in the scripts.yaml file (noting that config.yaml has script: !include scripts.yaml within it.

#  Fountain Topup Switch Script:
  fountain_topup:
    alias: Fountain Topup
    description: 'Turn Fountain on for 1 minute'
    sequence:
      - service: automation.trigger
        data:
          entity_id: automation.topup_fountain

and for the switch in config.yaml

       fountain_topup:
         friendly_name: "Topup Fountain"
         value_template: "{{ is_state('automation.topup_fountain', 'off') }}"
         turn_off:
           entity_id: script.fountain_topup
           service: script.turn_on
         turn_on:
           entity_id: script.fountain_topup
           service: script.turn_on
#         icon_template: "mdi:fountain"

The only thing I cant work out is that when the switch is actually switched I cannot get the icon to turn yellow with state_color: true All my other switches do but this one fails.

Thanks men. appreciate it

Glad it’s working for you. This potentially doesn’t seem like the most straight-forward way to accomplish what you’re after - but I’ve learned with HA, that if it’s working, usually best to not mess with it :wink:

If you want to post more about the actual problem / scenario you’re trying to solve for / accomplish - we could potentially suggest a more streamlined way to get there - but again - your call!

Sure. I have an automation for an irrigation node to topup a fountain every morning for 1 minute. Sometimes however the water level in the fountain is low and needs an extra topup. So I wanted to create a frontend switch to execute the automation.

With @Burningstones help I managed to code it into a button which funtioned but was not the ‘eye candy’ I wanted.

Now I have this script and created a switch I have both functionality and it fits in with the rest of my frontend look.

What I would ultimately love would be a slider to turn on and adjust the run time of each sprinkler (I have 6). At the moment they all run on automation times, ie… sprinker/irrigation automation to run specific days/times.

However that is well and truly above my current technical ability.

Cheers.

FTFY

/s

Button card whose tap_action calls a script (not automation) that tops up the fountain. You may not even need the script and call the service directly from tap_action.

Yes I already had a button card. The button card functioned. You click on the icon and the automation worked. However, I was after a sliding on/off toggle switch that fitted in with the rest of my frontend. I could not workout (or even know if it is possible) to accomplish that with a button card.

Why did you choose to make it execute an automation (instead of a script)? Can you post this automation so we can see what it does? I’m curious to know what is the automation’s trigger.

I chose to use the automation because I have never written a script before and my knowledge of this component was limited.

  alias: Topup Fountain
  description: 1 minute fountain water topup
  trigger:
  - at: 07:30:00
    platform: time
  condition: []
  action:
  - data: {}
    entity_id: switch.zone3
    service: switch.turn_on
  - delay: 00:01:00
  - data: {}
    entity_id: switch.zone3
    service: switch.turn_off

I see, so this automation runs at 7:30 and, on occasion, you want to run it manually.

You want to use a switch, instead of a button. A switch typically has two states (on and off) but you have created a Template Switch where both of its states do the same thing (they run the same script). That’s unusual but I believe your goal was a switch that, when turned on, moves its toggle automatically back to off. You have achieved that by setting the value_template to something that will normally always evaluate to false. That means when the switch is turned on, the toggle will immediately return to off.

Is my interpretation of your Template Switch correct?


If my understanding of your requirements is correct, here’s another way to achieve the same thing. All it requires is that you:

  1. Create an input_boolean
  2. Modify your existing automation

It doesn’t need a script or a Template Switch. The input_boolean is displayed in the Lovelace UI as a toggle button (just like a Switch or Template Switch).

Here’s the input_boolean:

input_boolean:
  fountain_topup:
    name: Fountain Topup

The automation acquires an additional trigger (when the input_boolean turns on) and an additional action (turns off the input_boolean).

  alias: Topup Fountain
  description: 1 minute fountain water topup
  trigger:
  - at: 07:30:00
    platform: time
  - platform: state
    entity_id: input_boolean.fountain_topup
    from: 'off'
    to: 'on'
  condition: []
  action:
  - service: input_boolean.turn_off
    entity_id: input_boolean.fountain_topup
  - data: {}
    entity_id: switch.zone3
    service: switch.turn_on
  - delay: 00:01:00
  - data: {}
    entity_id: switch.zone3
    service: switch.turn_off

Hi @123 your assumption was correct and I have now moved to your changes.

I guess it would be fair comment to say that this way reduces the need to script anything with just a slight modification to the automation.

Thankyou. Thankyou all.

Just to ask a couple of dumb questions (maybe not the best forum but while were on the subject).

  • what is the most basic use for a script. (when we have automations)
  • in input_boolean just a way to create a yes / no option

cheers

1 Like

A sctipt just executes a sequence of actions. An automation has a trigger and sometimes conditions and then an action or a sequence of actions.

A problem with automations is if you have a delay or wait template that stops the automation for a certain amount of time, if the automation is triggered again during this “stopped” time, it skips conditions and delays and goes through the whole sequence of actions.