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.

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"andto: "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
- 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.
- Use
homeassistant.turn_off: If your list includes a mix of lights and switches, change the service fromswitch.turn_offtohomeassistant.turn_offto 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. - 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.
Botmonster Tech