How do I turn a script into a switch?

I’ve poked around the forums and can’t quite find anything that works. I have modded my Breville Espresso machine and popped a Shelly1 inside so I can turn it on and off (full guide coming). The switch is a momentary one though so I need to pulse the relay. The code below works just fine but me being fussy, I want to turn it into an on/off switch inside HA:

script:
  pulse_coffee_machine:
    sequence:
      - service: switch.turn_on
        entity_id: switch.coffee_machine
      - delay:
          milliseconds: 400
      - service: switch.turn_off
        entity_id: switch.coffee_machine

Anyone know how to do that? I tried a template switch but that seems to require a value_template from another source so not quite sure how to do that.

You mean like you flick the switch in the interface on, that script runs and the switch in the interface goes off?

If so just add the script itself to your lovelace (and add an alias to the script of whatever you want the switch to be called).

Hey Marc. Thanks for the feedback. I want a switch (assumed state) to be present in HA. When I turn it on, it runs the script and the coffee machine turns on. When I turn the switch off, it runs the same script again. Obviously I’ll have no way of actually knowing whether it is actually on or off without using another Shelly to measure the power consumption but it seem to be pretty reliable.

The trouble with just using the script is that once it’s run, it shows as off so I want a switch so I can show the assumed state.

1 Like

With ya.

In that case add an input_boolean to the config to ‘monitor’ the assumed state, and use a template switch…

input_boolean:
  monitor_coffee:

switch:
  - platform: template
    switches:
      pulse_coffee_machine:
        friendly_name: Coffee Pot
        value_template: "{{ states('input_boolean.monitor_coffee') }}" 
        turn_on: &toggle_coffee
          - service: switch.turn_on
            entity_id: switch.coffee_machine
          - service: input_boolean.toggle
            entity_id: input_boolean.monitor_coffee 
          - delay:
              milliseconds: 400
          - service: switch.turn_off
            entity_id: switch.coffee_machine
        turn_off: *toggle_coffee

I’ve used a yaml anchor to prevent repeating the exact same code for the turn_off part, because its neater :slightly_smiling_face:

5 Likes

You Sir are a freak’n legend! That worked. Never heard of an anchor before too so that is going to save some time going forward. Now to make that modding guide! Thanks again. :yum:

1 Like

Ha, careful, you’ll make me blush :blush:

Glad you’re sorted :+1:

1 Like

Based on @anon43302295 reply I was able to make a switch to close and open 4 screens. I have to apply delays because the screens are controlled by RF-link. The newly created switch screens, can be used by Alexa (hue emulation) : “Alexa turn on screens”. Thanks.

input_boolean:
  screens:
   name: "Screens"
   icon: mdi:glassdoor


switch:
  - platform: template
    switches:
      screens:
        value_template: "{{ states('input_boolean.screens') }}"
        turn_on:
          - service: input_boolean.turn_on
            data:
              entity_id: input_boolean.screens
          - service: cover.close_cover
            data:
              entity_id: cover.groot_raam       
          - delay:
                milliseconds: 5000
          - service: cover.close_cover
            data:
              entity_id: cover.klein_raam       
          - delay:
              milliseconds: 5000 
          - service: cover.close_cover
            data:
              entity_id: cover.boven_links       
          - delay:
              milliseconds: 5000  
          - service: cover.close_cover
            data:
              entity_id: cover.boven_rechts   
          
        turn_off:
          - service: input_boolean.turn_off
            data:
              entity_id: input_boolean.screens
          - service: cover.open_cover
            data:
              entity_id: cover.groot_raam       
          - delay:
              milliseconds: 5000
          - service: cover.open_cover
            data:
              entity_id: cover.klein_raam       
          - delay:
              milliseconds: 5000  
          - service: cover.open_cover
            data:
              entity_id: cover.boven_links       
          - delay:
              milliseconds: 5000  
          - service: cover.open_cover
            data:
              entity_id: cover.boven_rechts     
1 Like