Contents

Target the Triggering Entity in Home Assistant

When you have one automation watching multiple switches, you don’t need to hardcode which one to turn off. You can use the trigger object to dynamically target whichever device started the automation.

The YAML Configuration

Copy and paste this into your automations.yaml or the YAML editor in the UI. Replace the entity IDs with your own.

alias: "Turn off the switch that triggered this"
description: "Automatically turns off the specific switch that was just turned on"
trigger:
  - platform: state
    entity_id:
      - switch.kitchen_light
      - switch.living_room_light
      - switch.garage_fan
    from: "off"
    to: "on"
condition: []
action:
  - service: switch.turn_off
    target:
      entity_id: "{{ trigger.entity_id }}"

How it works

  • The Trigger: We list all the switches we want to monitor. By specifying from: "off" and to: "on", we ensure the automation only runs when someone turns a switch on.
  • The Template: Instead of typing a specific entity name in the action, we use {{ trigger.entity_id }}.
  • Dynamic Response: Home Assistant automatically fills that template with the ID of the switch that actually changed state.

Quick Tips for Success

  1. Don’t click “Run” in the UI: If you test this by clicking the “Run” button in the Home Assistant interface, it will fail. This is because there is no actual “trigger” event to provide an ID. You must flip the physical switch to test it.
  2. Use homeassistant.turn_off: If your list includes a mix of lights and switches, change the service from switch.turn_off to homeassistant.turn_off to ensure it works for both types of devices.
  3. Avoid Loops: Always define the to: state. If you leave it blank, the automation might fire when the switch turns off, try to turn it off again, and create an error loop.