I’m writing a script and have added a device selector field and wish to get the state of a specific entity on the selected device.
More specifically, I have several Ulanzi TC001 clock’s around the house flashed with AWTRIX Light. I’m writing a script to allow me to construct a messages to be displayed on a clocks via the mqtt.publish
service with a topic of <device_topic>/notifiy
. The device_topic
for each device is available as an entity on the device (i.e. sensor.living_room_device_topic
, sensor.kitchen_device_topc
, etc.). In my script I have a selector for a device (filtered to only show the clocks) and would like to extract the MQTT device topic from the selected device.
I’ve managed to come up with the following that works.
alias: Awtrix Send Notification
description: Display a notification on an AWTRIX powered device
icon: mdi:alphabetical-variant
mode: parallel
fields:
device_id:
name: Device
description: Device on which to show the notification
required: true
selector:
device:
filter:
- integration: mqtt
manufacturer: Blueforcer
model: AWTRIX Light
# ADDITIONAL FIELDS OMITTED FOR BREVITY
sequence:
- service: mqtt.publish
data:
topic: "{{ states(device_entities(device_id) | select('contains', 'device_topic') | first) }}/notify"
payload_template: # OMITTED FOR BREVITY
Is there a better way of retrieving the device topic?