How to: simple on/off switch with two scripts

Hi,
I’m new here and would really appreciate your help as I’ve searched these forums and the net for days!
I have a light that is turned on and off via IR (Broadlink RM mini). I have created two scripts - one for on and one for off under the names:

  • script.spots_office_on
  • script.spots_office_off

All I want to do is to be able to have these in my overview with a toggle to turn the Office Spots on or off. There is no state sensor so really just a simple on/off toggle using the two scripts.
Any help appreciated.
Many thanks

Wood,

If I follow you, you can add an Identity card to the dashboard, and add to that your two scripts. You should then be able to activate either with a press on “Run”.

This shows adding a script entity to current entries in an Entity Card (bottom left) that sets 7 Nest thermostats for me, and the result for the dashboard Entities card on the right. FWIW, the entities showing as able to Activate are scenes. The entities with a toggle are light groups (for give the momentary Capt.Obvious here [g]).

Hope that helps.

Hi Bob,
Thanks for coming back to me. I can do that no problem., however what I would like to do is to have a toggle show up for on and off (see attached mockup) rather than two seperate lines with “run”
Screenshot HA

Thanks again!

Try going to Settings / Devices / Helpers, and create a Helper. Choose Group, Light Group.

Give it a name, then begin adding the spot lights you want toggle. If you have no need to see the lights in the group evident anywhere else (the light group alone is all you need), you can Hide Members of the group. Not necessary to decide on this right away. When the group is complete, save it.

Add that lighting group to your Entities Card.

configuration.yaml

switch:
  - platform: template
    switches:
      office_spots:
        friendly_name: Office Spots
        turn_on:
          - script.spots_office_on
        turn_off:
          - script.spots_office_off
1 Like

Also note, if you don’t use those scripts anywhere else you can put the script sequences directly in the template switch turn_on and turn_off options. They accept sequences of services (which is why I showed them above as lists with “-”).

1 Like

thanks Tom!
I added this to the configuration.yaml as posted but it does not show up in my entities? Anything I’m missing?

Also I’m not clear on your second post with reference to the “-”. I have about 20 on/off scripts that are linked to IR broadlink, so will need this configuration for all of them - so not a one off.
thanks again!

This is the error using the lines you posted:

Logger: homeassistant.config
Source: config.py:1258
First occurred: 11:46:13 (1 occurrences)
Last logged: 11:46:13
Invalid config for ‘switch’ from integration ‘template’ at configuration.yaml, line 97: expected dictionary ‘switches->office_spots->turn_on->0’, got ‘script.spots_office_on’, please check the docs at Template - Home Assistant Invalid config for ‘switch’ from integration ‘template’ at configuration.yaml, line 99: expected dictionary ‘switches->office_spots->turn_off->0’, got ‘script.spots_office_off’, please check the docs at Template - Home Assistant

thanks Bob for your reply, but that doesn’t work as the lights are captured as scripts and not lights. as such they are not recognized as light entities, so can’t be added to the light group.

Try this:

switch:
  - platform: template
    switches:
      office_spots:
        friendly_name: Office Spots
        turn_on:
          action: call-service
          service: script.spots_office_on
          target: {}
        turn_off:
          action: call-service
          service: script.spots_office_off
          target: {}

Thanks but does not show up as entity either! do i need to add anything between {}?

Sorry should be:

switch:
  - platform: template
    switches:
      office_spots:
        friendly_name: Office Spots
        turn_on:
          - service: script.spots_office_on
        turn_off:
          - service: script.spots_office_off

Also this goes in your configuration.yaml file and you have to restart home assistant after adding it.

1 Like

Why not creating a light group to switch on and off?

1 Like

Because they are not lights. They are infra-red commands.

Nothing stopping them using the Switch-as-X helper to convert the template switch into a light though.

1 Like

Perfect, works, thanks Tom! Appreciate yours and everyone else’s help and advice!

will google this solution - probably the more advanced/better option to make it a “light”!

1 Like

Bowing to Wood & Tom, this topic lead me to make the time to find/view some YTube tutorials on Templates. I suppose I wasn’t ready until now to delve into them (didn’t know what to do with the answers here, despite HA help links), but I’m starting to grok it now.

:ok_hand: