Triggering automation from entity extracted from device

I’m trying to create a blueprint that will allow the user to select a device and then have the blueprint trigger off of an entity within that device. Example: user selects a power monitoring plug and the blueprint will identify (automatically) and trigger off of the power sensor and then turn off the plug.

The problem I have is that the power sensor entity is determined by a template filter and I can’t use it as an entity_id in the automation trigger.

Code:

blueprint:
  name: Turn off plug
  domain: automation
  input:
    plug_device:
      name: Smart plug 
      selector:
        device:
          entity:
          - device_class:
            - power
          - domain: switch
  
variables:
  plug_device_id: !input plug_device
  plug_power_entity_id: "{{ device_entities(plug_device_id) | select('match', '.*_energy_power') | first }}"
  
mode: single
trigger:
  - platform: numeric_state
    entity_id: "{{plug_power_entity_id}}"

The issue is in the very last line.

This will not work because templates in triggers are limited templates, but variables are not rendered until after the trigger. If you want to try to create a variable for a trigger you need to do it in a trigger_variable. However I believe the match function is not available in limited templates anyway, so you will have to trigger more generally, like trigger when something happens with the device, then use variables or conditions to do the action only when your desired conditions are met.
Trigger is all limited templates, where variables and conditions and things like choose statements have the whole HA template tool set available.

Inside a BP, no matter the order they show up in the BP code, things are executed in this order:
!inputs, trigger_variables, triggers, variables, conditions then actions.
Things on the left do not know things on the right exist.

Thanks James.

Is it possible to have a device trigger for any change in the device? At this point I only have the power sensor entity id in a variable, so I can’t provide that. The docs for this are pretty sparse and seem to suggest I need an entity id.

And to confirm: can conditions use templates for entity ids?

I’m trying this but it still requires an entity id:

trigger:
  - platform: device
    device_id: !input plug_device
    domain: sensor

Any ideas?

remove domain
(maybe)

Complains without domain too.