Contents

Target the Triggering Entity in Home Assistant

Contents

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.

Home Assistant automation editor showing a configured trigger with entity and threshold settings
The Home Assistant automation editor provides a visual interface, but the YAML editor offers more precise control over trigger objects

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. The same pattern scales naturally to any state-reporting device — if you are monitoring your home’s energy usage via Home Assistant, you can fire automations based on whichever circuit changes 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. This same dynamic targeting works equally well with ESPHome-based sensors — a single automation can respond to readings from any sensor in the group rather than hardcoding each one individually.
  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. More complex multi-entity setups — like those powering local AI security camera automations with Frigate — rely on these same loop-prevention principles to keep multiple detection zones firing cleanly.