Motion Sensors - Cookie cutter approach for many sensor/light actions

Hi all,

Following the cookbook example Motion sensor light on/off after 10 minutes works a treat.

Now the heart of this question is, as anyone who works with code is duplication is evil.
Assuming I have 3-5 “rooms” with sensors and lights, how to simply substitute the trigger and action?
There must be something, either a script or somehow to reduce duplication, even at the dumbest level an array or dictionary would substitute.

If I’m barking up the wrong tree entirely, then please let me know. This stuff is mainly a mixture of Hue and ESPHome (both for the lights and sensors, though I find the Hue sensors a bit slow and might look to replace them where possible).

Thanks in advance.

Templating would work, though it might look a little ugly in the action part with something like:

- service: light.turn_off
  data_template:
    entity_id: >-
      {% if trigger.entity_id = binary_sensor.hall_motion %}
        light.hall
      {% elif trigger.entity_id = binary_sensor.kitchen_motion %}
        light.kitchen
      {% elif ... %}
...

That’s definitely a good start, is that Jinja syntax?
If we can pass a dictionary (map) that could look really elegant.

Here’s what I’ve been trying out lately. It’s not thoroughly tested, but I think it should work. I will admit, I didn’t come up with this, I’d copied it from somewhere else, but I can’t find the source…

- alias: "motion lights"
  trigger:
    - platform: state
      entity_id:
        - sensor.foyer_pir
        - sensor.kitchen_pir
        - sensor.basement_pir
        - sensor.bar_pir
        - sensor.storage_pir
      to: 'Violated'
    - platform: state
      entity_id:
        - sensor.foyer_pir
        - sensor.kitchen_pir
        - sensor.basement_pir
        - sensor.bar_pir
        - sensor.storage_pir
      to: 'Normal'
      for:
        minutes: 10
  condition: ## Ensure Motion Lights aren't disabled
    - condition: state
      entity_id: input_boolean.block_all_motion_lights
      state: 'off'
  action:
    service_template: >
      {% if trigger.to_state.state == 'Violated' %}homeassistant.turn_on
      {% else %}homeassistant.turn_off{% endif %}
    data_template:
        entity_id: >
          {% set trigger_entity = trigger.entity_id %}
          {% if trigger_entity == 'sensor.foyer_pir' %}
            switch.exterior_foyer
          {% elif trigger_entity == 'sensor.kitchen_pir' %}
            switch.kitchen_cabinets
          {% elif trigger_entity == 'sensor.basement_pir' %}
            switch.basement_main
          {% elif trigger_entity == 'sensor.bar_pir' %}
            switch.basement_bar_cabinets
          {% elif trigger_entity == 'sensor.storage_pir' %}
            switch.basement_storage_lights
          {% endif %}

I offer you this streamlined code for data_template:

    data_template:
      entity_id: >
        {% set map = {'foyer_pir': 'exterior_foyer',
                      'kitchen_pir': 'kitchen_cabinets',
                      'basement_pir': 'basement_main',
                      'bar_pir': 'basement_bar_cabinets',
                      'storage_pir': 'basement_storage_lights'} %}
        {% set id = trigger.to_state.object_id %}
        switch.{{ map[id] if id in map.keys() else 'none' }}

Thank you! That’s kind of what I had in mind, though I couldn’t quite grok the syntax.
I’ll definitely give this a go tonight.
Assuming this works, it elegantly highlights what differs room to room, providing an easy visualisation and change.

If you have a mix of switches and lights then the template needs to change to this:

    data_template:
      entity_id: >
        {% set map = {'foyer_pir': 'switch.exterior_foyer',
                      'kitchen_pir': 'light.kitchen_cabinets',
                      'basement_pir': 'light.basement_main',
                      'bar_pir': 'switch.basement_bar_cabinets',
                      'storage_pir': 'light.basement_storage_lights'} %}
        {% set id = trigger.to_state.object_id %}
        {{ map[id] if id in map.keys() else 'none' }}