Bart Kessels
Bart Kessels
Passionate open source software engineer who loves to go backpacking

Easily turn on or off multiple lights

Easily turn on or off multiple lights
This image is generated using Dall-E
  • Prompt: Generate an image of a person trying to control multiple lights in a small home in the dark in a minimalistic flat style
  • Introduction

    Recently I was creating an automation where I wanted to turn of multiple lights at once. The automation was supposed to turn off all the lights in the living room and the kitchen when the sun comes up. The hallway lights still needed to be on because there’s no sunlight in the hallway. Therefore, I couldn’t just run the turn off all lights voice command.

    The verbose method

    Now is this very easy using the yaml automations where you can just copy the action and change the name of the light, but to me this didn’t look very pleasing having more than five lights. Because it looked like this.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    
    # ... redacted
    
    automations:  
      # ... redacted
    
      actions:
        - type: device.command.OnOff
          on: false
          devices: Hallway light - Hallway
        - type: device.command.OnOff
          on: false
          devices: Front light - Living Room
        - type: device.command.OnOff
          on: false
          devices: Back light - Living Room
    

    The cleaner method

    After some playing with the onOff action and trying to use the collection syntax of yaml (YAML Language Development Team, 2021). And if we look at the documentation that Google provides, we see that the device actions support the use of multiple devices with the collection syntax (Google, n.d.).

    So if we use the collections instead of copying the action for each light our yaml file will look like this.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    
    # ... redacted
    
    automations:  
      # ... redacted
    
      actions:
        - type: device.command.OnOff
          on: false
          devices:
            - Hallway light - Hallway
            - Front light - Living Room
            - Back light - Living Room
    

    As you see, this looks a lot cleaner than our previous verbose method of copying each action.

    Categories

    Related articles

    and condition in Google Home

    Make your Google Home even smarter by adding an AND condition to your custom automation.

    or condition in Google Home

    Upgrade your automations by adding an OR condition.

    Keep your smart plug turned on

    Keep your Philips Hue smart plug turned on even when you ask Google to turn off all the lights.

    Keep your automations clean

    Keep your automations overview clean by adding multiple automations in the same yaml-file.

    Naming your automations

    Keep your automations yaml clean by adding names to each automation.