#Help creating an automation on an event for multiple devices by label?

1 messages · Page 1 of 1 (latest)

winged light
#

Hi all, I have some code below that I use to get notification of a printer event (print finished). However, I was looking to add more printers, and wanted to know how I can convert this automation to be more dynamic. I'd like to maybe use labels on the printer (eg, automation-enabled) for new printers and not have to modify the trigger in the future. If you can suggest some revision to this, it would be appreciated. ChatGPT wasn't very helpful...

`
alias: 3D Print Completed
description: Notify when 3d print is completed
triggers:

  • device_id: aab90c680f0b33435d25cdeb87914666
    domain: bambu_lab
    type: event_print_finished
    trigger: device
  • device_id: def4d8e0ee2d097ae5a1674b4eb5e666
    domain: bambu_lab
    type: event_print_finished
    trigger: device
    actions:
  • action: notify.alexa_media_echo_show
    metadata: {}
    data:
    message: The printer {{ trigger.event.data.name }} has finished printing.
    mode: single
    `
faint lion
#

For a more generic trigger you will likely need to switch to an Event trigger instead of Device triggers. I don't have a bambu labs printer, so I have no way to check this in the Event tool, but the trigger would be something like:

triggers:
  - trigger: event
    event_type: event_print_finished
    event_data:
      domain: bambu_lab
conditions:
  - condition: template
    value_template: |
      {{ trigger.event.data.device_id in label_devices('automation_enabled') }}

Keep in mind that the template used above requires that the label be applied to the device, not just one of the entities from the device. If that is not an option, you can use:

{{ 'automation_enabled' in device_entities(trigger.event.data.device_id)
| map('labels') | sum(start=[]) | list }}
winged light
#

I will give this a shot. Thanks for the assist!

winged light
# faint lion For a more generic trigger you will likely need to switch to an Event trigger in...

So I tried just to reduce teh trigger to just this (no conditions for now):
`
alias: 3D Printing Done - Test Alt Template
description: Notify living room when 3d print is completed
triggers:

  • trigger: event
    event_type: event_print_finished
    event_data:
    domain: bambu_lab

...
`

And unfortunately that didn't work. I did look through a trace for a prior trigger based on device, and saw the below in the trace > Changed variable. I'm still new to HA scripting, but does this offer any clue on how to retweak the code? :

`
this:
entity_id: automation.3d_print_completed
state: 'on'
attributes:
id: '1733098523773'
last_triggered: '2024-12-18T03:20:13.665325+00:00'
mode: single
current: 0
friendly_name: 3D Print Completed
last_changed: '2024-12-17T03:50:32.229292+00:00'
last_reported: '2024-12-18T03:20:13.667133+00:00'
last_updated: '2024-12-18T03:20:13.667133+00:00'
context:
id: 01JFBWFH31SMWVFT8FCSHZH6J2
parent_id: 01JFBWFH2YB8JZ4Z69Q43932WJ
user_id: null
trigger:
id: '1'
idx: '1'
alias: null
platform: device
event:
event_type: bambu_lab_event
data:
device_id: c534d8e0ee2d097ae5a1674b4eb5e417
name: P1S-B
type: event_print_finished
origin: LOCAL
time_fired: '2024-12-18T03:20:30.122644+00:00'
context:
id: 01JFBWG15AHHH32M051F6TC34W
parent_id: null
user_id: null
description: event 'bambu_lab_event'

`

faint lion
#

Ok, that's exactly the information that is needed... you should use the event_type and type from the trace as follows:

triggers:
  - trigger: event
    event_type: bambu_lab_event
    event_data:
      type: event_print_finished
conditions:
  - condition: template
    value_template: |
      {{ trigger.event.data.device_id in label_devices('automation_enabled') }}
actions:
  - action: notify.alexa_media_echo_show
    metadata: {}
    data:
      message: The printer {{ trigger.event.data.name }} has finished printing.
mode: queued