One single automation for multiple light and motion sensors

Tags: #<Tag:0x00007f7c5c6c27f0>

Hello everyone;
i’m wondering if it would be possible to create a single automation to turn off and on lights in multiple rooms.
I would like to use a variable 1: %motion sensor X and a variable 2: %light X.
Once %motion sensor X is triggered, %light X turns on
As followed:

alias: PIR triggers Light generic
  trigger:
    - platform: state
      entity_id: sensor.%PIR_X
      to: 'on'
  action:
    - service: light.turn_on
      data:
        entity_id: light.%BULB_X

Does anyone knows if its possible to do that?
Thanks a lot for your help!
Gussir

Yes with groups.

I don’t think groups are the solution in this case because all lights of the group would be turned on or off at the same time.

You could use every motion sensor as a trigger in your automation and in the action you could check which motion sensor has been triggered and then turn on the correct light.

- service: light.turn_on
  data_template:
    entity_id: >-
      {% if trigger.entity_id == binary_sensor.motion_livingroom %}
        light.livingroom
      {% elif trigger.entity_id == binary_sensor.motion_kitchen %}
        light.kitchen
      {% endif %}

Hello, Thanks for the feedback.
It doesn’t seem to work as expected.
As trigger should I put all of my motion sensors in a single automation ?

This is possible, but as lights are all over the house you can not be sure of non simultaneity (ie you switch a light downstairs whilst your wife switches a light upstairs). This is mainly if there were any timers/delays involved.
It would require VERY strict naming conventions.
Example
sensor.bedroom01
light.bedroom01
Etc.
So that in the action phase you could extract the name of the pir and apply it to the light.
Then the turning off of the light then becomes a nightmare for the usual scenarios with timers on loss of motion etc and would require separate automations anyway.
It’s not worth it.

If you are doing this to save time writing code then examine packages

If you literally just trying to cut down on automations, it’s not worth it as it will be difficult to maintain.

Edit: why is this listed under hardware rather than configuration ?