Template not expanding in blueprint

I’m working on my first blueprint. I’ve maybe picked something too complicated, but my ultimate goal is simplify deploying a pair of automations to a group of light switches. What I would love to have in my blueprint is a selection of multiple switches and it automatically applies the pair of automations to each light, though I can understand if I need to have one blueprint per automation.

Automation 1 as YAML:

alias: Bedroom light to full if tapped on second time
description: ""
trigger:
  - platform: device
    device_id: 46f9d5ed0404431987790ee034b8cd95
    domain: zwave_js
    type: event.value_notification.central_scene
    property: scene
    property_key: "002"
    endpoint: 0
    command_class: 91
    subtype: Endpoint 0 Scene 002
condition:
  - condition: device
    type: is_on
    device_id: 46f9d5ed0404431987790ee034b8cd95
    entity_id: light.master_bedroom_light
    domain: light
    for:
      hours: 0
      minutes: 0
      seconds: 1
action:
  - type: turn_on
    device_id: 46f9d5ed0404431987790ee034b8cd95
    entity_id: light.master_bedroom_light
    domain: light
    brightness_pct: 100
    enabled: true
mode: single

Automation 2 as YAML

alias: Bedroom light to low if tapped off second time
description: ""
trigger:
  - platform: device
    device_id: 46f9d5ed0404431987790ee034b8cd95
    domain: zwave_js
    type: event.value_notification.central_scene
    property: scene
    property_key: "001"
    endpoint: 0
    command_class: 91
    subtype: Endpoint 0 Scene 001
condition:
  - condition: device
    type: is_off
    device_id: 46f9d5ed0404431987790ee034b8cd95
    entity_id: light.master_bedroom_light
    domain: light
    for:
      hours: 0
      minutes: 0
      seconds: 1
action:
  - type: turn_on
    device_id: 46f9d5ed0404431987790ee034b8cd95
    entity_id: light.master_bedroom_light
    domain: light
    brightness_pct: 25
    enabled: true
mode: single

My current attempt at just blueprinting automation 1 with the input of a single light switch

blueprint:
  name: Light to full when turned on again
  description: Pressing the on button of a light switch will set the light to full brightness
  domain: automation
  input:
    target_light:
      name: Light switches
      description: The light switches to add shortcuts to (currently only supports Inovelli Red switches)
      selector:
        entity:
          domain: light

trigger_variables:
  target_light: !input target_light
  target_light_device_id: "{{ device_id(target_light) }}"

trigger:
  - platform: device
    entity_id: !input 'target_light'
    device_id: "{{ target_light_device_id }}"
    domain: zwave_js
    type: event.value_notification.central_scene
    property: scene
    property_key: "002"
    endpoint: 0
    command_class: 91
    subtype: Endpoint 0 Scene 002

variables:
  target_light: !input target_light
  target_light_device_id: "{{ device_id(target_light) }}"

action:
  - type: turn_on
    device_id: "{{ target_light_device_id }}"
    entity_id: !input 'target_light'
    domain: light
    brightness_pct: 100
    enabled: true

When I try to create an automation based on this template I get this error:

Message malformed: Unknown device '{{ target_light_device_id }}'

It seems like the template that uses the variable isn’t being evaluated out. Since this is my first blueprint I’m sure I’m making a silly error. Anything standing out to anyone?

You cannot transfer the value of an input directly into a template. You have to use a variable for that.
But in this case, you can use a device selector instead and then you don’t need all the conversions you are doing.

Try something like this:

blueprint:
  name: Light to full when turned on again
  description: Pressing the on button of a light switch will set the light to full brightness
  domain: automation
  input:
    target_light:
      name: Light switches
      description: The light switches to add shortcuts to (currently only supports Inovelli Red switches)
      selector:
        device:
          domain: light
          integration: zwave  ### please change to the right name of zwave integration

trigger:
  - platform: device
    device_id: !input target_light
    domain: zwave_js
    type: event.value_notification.central_scene
    property: scene
    property_key: "002"
    endpoint: 0
    command_class: 91
    subtype: Endpoint 0 Scene 002

action:
  - service: light.turn_on
    data:
      brightness_pct: 100
    target:
      device_id: !input target_light

You probably can use multiple devices on your input, so you don’t need one automation per light but in that case you will have to change the call for the light.turn_on service and play with the trigger variable as that one probably contains the entity or device triggering the event.
Try to explain better what you are trying to achieve with your automation (specially what is your trigger) and we can try to help more.

Thanks for the suggested edit. Changing to the light.turn_on service got me further as I can keep it all in the device_id.

For my automation, I want to be able to press the “on” button on my light switch while the light is already on but not at full brightness to cause the light to go to full brightness. When I press the “off” button and the light is already off, I want it to turn on to a dim setting around 25% (Ideally this is configurable in the blueprint too).

I suspect these will have to be 2 different blueprints, one for the on button and one for the off button, but I’m hoping to be proven wrong there.

I don’t see a problem about having the 2 commands in a single blueprint. You can use Trigger Id and choose action to have your blueprint adapting to the button pressed.
But before going to that direction, let me understand better your trigger. You mentioned you have a switch to turn in the light… Could you please share the model you are using or maybe the entities (if any) you have available for that device (a screenshot?).

My smart switches are Inovelli Red Dimmers. Here is a screenshot from the Z-Wave JS device page:

And with some templating filters I have this list of entity IDs:

['light.master_bedroom_light', 'sensor.master_bedroom_light_electric_consumption_w_2', 'sensor.master_bedroom_light_electric_consumption_kwh', 'select.master_bedroom_light_local_protection_state', 'select.master_bedroom_light_rf_protection_state', 'sensor.red_series_dimmer_node_status_3', 'button.red_series_dimmer_ping_3', 'update.master_bedroom_light_firmware']

I have also started on the condition required for Automation 1. This is needed so pressing the “on” button when the light is off doesn’t fire the action.

blueprint:
  name: Light to full when turned on again
  description: Pressing the on button of a light switch will set the light to full brightness
  domain: automation
  input:
    target_light:
      name: Light switches
      description: The light switches to add shortcuts to (currently only supports Inovelli Red switches)
      selector:
        device:
          entity:
            domain: light


trigger:
  - platform: device
    device_id: !input target_light
    domain: zwave_js
    type: event.value_notification.central_scene
    property: scene
    property_key: "002"
    endpoint: 0
    command_class: 91
    subtype: Endpoint 0 Scene 002

variables:
  target_light: "{{ trigger.event.data.device_id }}"
  target_light_entity_id: "{{ device_entities(target_light) | select('match', 'light') | first }}"

condition:
  - condition: device
    type: is_on
    device_id: "{{ target_light }}"
    entity_id: light.master_bedroom_light
    domain: light
    for:
      hours: 0
      minutes: 0
      seconds: 1

action:
  - service: light.turn_on
    data:
      brightness_pct: 100
    target:
      device_id: "{{ target_light }}"
  - service: notify.bens_devices
    data:
      message: "{{ target_light_entity_id }}  {{ trigger.event.data.device_id }}"

This works, but I have the condition’s entity_id hard coded. If I swap that out to "{{ target_light_entity_id }}" I get an error:

Message malformed: Entity ID {{ target_light_entity_id }} is an invalid entity ID for dictionary value @ data['entity_id']

Putting that same "{{ target_light_entity_id }}" into a notification shows the expected value of light.master_bedroom_light, so I know my variable is being populated correctly. So for some reason the device_id is fine with a template but entity_id seems to not be.

You should avoid working with entity_id and device_id for the same item. It is possible, but why not choosing the easier path?
I would always chose the entity_id as the device_id are not under your control (and can be an issue when you replace a device).

So, I would try something like this:

blueprint:
  name: Light to full when turned on again
  description: Pressing the on button of a light switch will set the light to full brightness
  domain: automation
  input:
    target_light:
      name: Light switches
      description: The light switches to add shortcuts to (currently only supports Inovelli Red switches)
      selector:
        entity:
          domain: light

trigger:
  ## You can capture any event from this device to trigger your automation
  - platform: event 
    event_type: zwave.scene_activated
    event_data:
      entity_id: !input target_light

action:
  - choose:
      - conditions: # when the up button was pressed with the lights on
          - "{{ trigger.event.data.scene_id == 2 }}"  #up button pressed
          - condition: state
            entity_id: !input target_light
            state: "on"
        sequence:
          - service: light.turn_on
            data:
              brightness_pct: 100
            target:
              entity_id: !input target_light
      - conditions: # when the down button was pressed with the lights off
          - "{{ trigger.event.data.scene_id == 1 }}"  #down button pressed
          - condition: state
            entity_id: !input target_light
            state: "off"
        sequence:
          - service: light.turn_on
            data:
              brightness_pct: 10
            target:
              entity_id: !input target_light
# I believe you will remove this notification with the final version, but if you want to keep add an if..then to only send notifications with the selected events.
  - service: notify.bens_devices
    data:
      message: "{{ target_light_entity_id }}  {{ trigger.event.data.device_id }}"

Thank you for this suggestion. I didn’t previously see a way to do the trigger without the device_id, but I will try your method soon.

I don’t have a device like yours, so I cannot duplicate your case, but usually you can filter the event by whatever you want, so you can choose between device or entity. I will always go with entities if possible.
If you have to use both then I agree you should yse the variables as you tried, otherwise I will pick one (device or entity) and try to work only with that one. The service to turn on the light is flexible enough for your choice.

Looking in the developer tools events section, if I set it to listen to * events, the only event that seems to relate to my switch mentions the device_id but not the entity_id.

event_type: zwave_js_value_notification
data:
  domain: zwave_js
  node_id: 17
  home_id: 3552647087
  endpoint: 0
  device_id: 46f9d5ed0404431987790ee034b8cd95
  command_class: 91
  command_class_name: Central Scene
  label: Scene 002
  property: scene
  property_name: scene
  property_key: "002"
  property_key_name: "002"
  value: KeyPressed
  value_raw: 0
origin: LOCAL
time_fired: "2023-02-25T03:00:17.210591+00:00"
context:
  id: 01GT3876NTE3K1NMTHPDYJ4R6A
  parent_id: null
  user_id: null

I will see if I can get the condition working with only the device_id.

Okay, I’ve got it working for a single switch. Since I can’t seem to use a template for entity_id and I can’t use the device_id() filter in the limited templates used by trigger variables, I set the trigger to be any Z-Wave button press and then added a condition to confirm its device ID matches the one for the user provided entity.

blueprint:
  name: Light to full when turned on again
  description: Pressing the on button of a light switch will set the light to full brightness
  domain: automation
  input:
    target_light:
      name: Light switches
      description: The light switches to add shortcuts to (currently only supports Inovelli Red switches)
      selector:
        entity:
          domain: light

trigger:
  - platform: event
    event_type: zwave_js_value_notification
    event_data:
      value: KeyPressed

variables:
  triggered_light: "{{ trigger.event.data.device_id }}"
  target_light: !input target_light
  target_light_entity_id: "{{ target_light }}"
  
condition:
  - "{{ trigger.event.data.device_id == device_id(target_light) }}"

action:
  - choose:
      - conditions:
          - "{{ trigger.event.data.property_key | int == 2 }}"  #up button pressed
          - condition: state
            entity_id: !input target_light
            state: "on"
        sequence:
          - service: light.turn_on
            data:
              brightness_pct: 100
            target:
              entity_id: !input target_light
      - conditions:
          - "{{ trigger.event.data.property_key | int == 1 }}"  #down button pressed
          - condition: state
            entity_id: !input target_light
            state: "off"
        sequence:
          - service: light.turn_on
            data:
              brightness_pct: 10
            target:
              entity_id: !input target_light

The next tricky bit will be adding multiple different switches to a single automation. I guess I would have to map the entity IDs to their corresponding device IDs, confirm the device ID is in the list, then somehow get the state of the entity without using templates…

I’m open to suggestions there, but I probably need to file a bug report.