Visualize Scripts as Switches

Hi all, great community!
I’m not properly a noob but somehow there’s a particular simple (I think) customization I’m not able to get right.
I have 2 simple scripts to turn on and off respectively a Sonoff wireless outlet via IFTTT (firmware not flashable unfortunately). Both show fine in my group, however I wanted to have them show like the rest of the switches, meaning a single line with two “lightning bolts” for on and off.
I’ve tried template, cover but no success. The most I could get was a toogle button that actually did nothing.
Current layout:

Hoping there’s a simple way so thanks in advance for your input.

Have a look at this https://www.home-assistant.io/components/switch.template/

Hi, thanks for the swift reply.
Already did and I can’t figure out what to do with the “value_template” required option. These are not scripts or switches that depende on a certain condition, all I want is to click them to have an action performed.
Moreover, that particular configuration creates a toggle switch, not the ones I’m looking for.

value_template:  "{{ is_state('script.master_ha','on') }}"
switch:
  - platform: template
    switches:
      master_ha:
        value_template: "{{ is_state('script.master_ha','on') }}"
        turn_on:
          - service: script.master_ha
        turn_off:
          - service: script.turn_off
            entity_id: master_ha
1 Like

Looks like you beat me to it @petro :+1:
@xbyt that should work, and you can also use customize to set assumed_state to get the lightning icons

1 Like

So good news and bad news.
The good news is that visualy it is now exactly as I want it. Thanks @ludeeus for the tip on the assumed_state
Capture

The bad is that it doesn’t work, none of the buttons has any action on the device I want to control, here’s my code:

  - platform: template
    switches:
      rs_sonoffplug:
        value_template: "{{ is_state('script.soplug_on', 'on') }}"
        turn_on:
          service: switch.turn_on
          data:
            entity_id: script.soplug_on
        turn_off:
          service: switch.turn_off
          data:
            entity_id: script.soplug_off

script.soplug_on and script.soplug_off are working scripts. I can call them directly in the “Fountain On” and “Fountain Off” ACTIVATE buttons:

soplug_on:
  alias: "Fountain On"
  sequence:
  - service: ifttt.trigger
    data: {"event":"HA_SOPlugON"}

soplug_off:
  alias: "Fountain Off"
  sequence:
  - service: ifttt.trigger
    data: {"event":"HA_SOPlugOFF"}

I feel that I’m missing something quite simple, but don’t know what

You can’t use switch.turn_* for scripts.
You need to use homeassistant.trun_on / homeassistant.trun_off for that :slight_smile:

or even shorter:

 - platform: template
    switches:
      rs_sonoffplug:
        value_template: >
          {{ is_state('script.soplug_on', 'on') }}
        turn_on:
          service: script.soplug_on
        turn_off:
          service: script.soplug_off

though the value_template is a bit awkward. This might be a case for an input_boolean, and an automation with

trigger:
  platform: state
  entity_id: input_boolean.soplug
action:
  service_template: >
    script_soplug_{{states('input_boolean.so_plug')}}

no?

if yes, (and you have installed custom-ui ) do fun stuff like:

homeassistant:
  customize:
    input_boolean.so_plug:
      templates:
        icon: >
          if (state === 'on') return 'mdi:water';
          return 'mdi:water-off';
        icon_color: >
          if (state === 'on') return 'rgb(251, 210, 41)';
          return 'rgb(54, 95, 140)';

Guys! Thanks
Actually both @ludeeus and @Mariusthvdb solutions work being the last one the shortest and “cleanest”.
The trick was to replace service: switch.turn_on by service: homeassistant.turn_on as @ludeeus mentioned or go directly to the service like @Mariusthvdb added in the last post.