(Solved) Use switch template to run scripts?

Hi!
I’m a bit of a noob when et comes to custom config.
I have som cheap RF Switches controlling some non-smart lamps in my office.

These switches does not respond well to multiple commands, and therefore i can’t turn multiple on and off at the same time.

Therefore i have made some simple scripts, with some delay:

'OfficeON':
  alias: Turn ON office lights
  sequence:
  - data:
      entity_id: light.strip
    service: light.turn_on
  - delay: 
      milliseconds: 500
  - data:
      entity_id: switch.kontakt_1
    service: switch.turn_on
  - delay: 
      milliseconds: 500
  - data:
      entity_id: switch.kontakt_2
    service: switch.turn_on
'OfficeOFF':
  alias: Turn OFF office lights
  sequence:
  - data:
      entity_id: light.strip
    service: light.turn_off
  - delay: 
      milliseconds: 500
  - data:
      entity_id: switch.kontakt_1
    service: switch.turn_off
  - delay: 
      milliseconds: 500
  - data:
      entity_id: switch.kontakt_2
    service: switch.turn_off

I want a single switch, like the normal lights switch, that can run the scripts, and controll all the lights at the same time.

My attempt after som googeling:

# Custom Office switch START #
switch:
  - platform: template
    switches:
      OfficeLight:
        value_template: "{{ is_state('input_boolean.OfficeLight', 'on') }}"
        turn_on:
          service: switch.turn_on
          data:
            entity_id: switch.OfficeLight
        turn_off:
          service: switch.turn_on
          data:
            entity_id: switch.OfficeLight

input_boolean:
  OfficeLight:
    name: OfficeLight
    inital: off

automation:
  turn_office_on:
   trigger:
    platform: state
    enitity_id: officeLights
    from: off
    to: on
  action:
    service: script.OfficeON
    entity_id: script.OfficeON

# Custom Office switch END #

This is giving me config errors, and i’m not hardcore enough to debug it.

Hope someone can give me a good solution on how to get this working! :slight_smile:

Have you thought of grouping them? Or is this not working and the reason why you’re raising this post?

for your script, your service is incorrect, it should be service: script.turn_on

alternatively, have you thought of creating an automation that would have multiple actions (namely one for each light with delays if needed). Only question is what is triggering the automation/script? you’d need an input or state to monitor example input_boolean.office_lights (try and stick to lowercase)

Hi.
I am just trying to get this working somehow.
As is, it does not work at all.

Maybe i dont understand the “Switch template” correctly, but i thought this would make a switch in the ui, that i can configure myself?

The Input_boolen should just be an variable telling the state of the office lights, since the lights cant respond their state?

And then the automation should trigger when the switch is triggered, and then trigger the script?

This is my understanding, but i really dont know if this is the best way or if this is even correct?

The switch template will take the status or attribute of another entity and create a new switch.
What you’re trying to achieve if I understand well is to create a new switch to control all your receivers.
If so an input_boolean would work, then you just need to create an automation which triggers on the state of that input.
I think you’re over-complicating it.
The first thing to know if if indeed your individual switches do return a status. If so you can simply group them together.

Here is an example of such an input that I have on my setup (I’ve added a delay for you):

- alias: Normal
  trigger:
    - platform: state
      entity_id: input_boolean.normal
      to: 'on'
  action:
    - service: tts.google_say
      entity_id: media_player.living_room_google_mini
      data:
        message: 'Scene, normal, activated'
    - service: light.turn_on
      data:
        entity_id: light.living_room
    - delay: 
        milliseconds: 500
    - service: light.turn_on
      data:
        entity_id: light.TV_Cabinet_LED_Strip
        brightness: 255
        rgb_color: [0,255,0]
        transition: 1
    - service: climate.set_away_mode
      data:
        entity_id: climate.hallway
        away_mode: false

Then create a similar automation for when you turn the switch off.
The only remaining question is:
Do each of your controlled socket return a status when changed? If so how do you want the other lights and your input_boolean to behave if you switch all your office lights but then switch just one lamp off?
You’ll probably need to create more automations to handle this

Ohh, i see, that i dont need the switch template then!

None of the lights/switches return a state.

Ill try to modify the conde you sendt me!

Awesome!
I got it working from your code!
So much simpler than what i was trying to do! xD

Thanks allot @lolouk44!

The final code:

Automations:

#OFFICE SWITCH AUTOMATION#
- alias: Turn office ON
  trigger:
    - platform: state
      entity_id: input_boolean.office_lights
      to: 'on'
  action:
    - service: light.turn_on
      data:
        entity_id: light.strip
    - service: switch.turn_on
      data:
        entity_id: switch.kontakt_1
    - delay: 
        milliseconds: 500
    - service: switch.turn_on
      data:
        entity_id: switch.kontakt_2

- alias: Turn office OFF
  trigger:
    - platform: state
      entity_id: input_boolean.office_lights
      to: 'off'
  action:
    - service: light.turn_off
      data:
        entity_id: light.strip
    - service: switch.turn_off
      data:
        entity_id: switch.kontakt_1
    - delay: 
        milliseconds: 500
    - service: switch.turn_off
      data:
        entity_id: switch.kontakt_2
#OFFICE SWITCH AUTOMATION# 

The Boolean:

# Custom Office Light Switch START #
input_boolean:
  office_lights:
    name: Kontor Lys
    initial: off
    icon: mdi:keyboard-variant
# Custom Office Light Switch END #
1 Like