I need to trigger an automation when a smoke alarm is triggered, and that works. But I’d like to filter the name of the alarm so I only get the actual room, since the MQTT topic already says that it’s a smoke alarm.I tried to start by removing the " - alarm" at the end, but I got stuck even there.
- id: '1360646345812'
alias: Smoke alarm
description: Send event on MQTT when a smoke alarm is triggered
trigger:
- event_data: {}
event_type: state_changed
platform: event
condition:
- condition: template
value_template: '{{ trigger.event.data.new_state.attributes.device_class == "smoke" }}'
- condition: template
value_template: "{{ trigger.event.data.new_state.state == 'on' }}"
action:
data_template:
payload_template: " {{ state_attr(trigger.event.data.entity_id, ('friendly_name.split(' - ')[0])') }} "
topic: eg/Smoke
service: mqtt.publish
I fear it’s something about the quotes in the payload template. I don’t get any error messages in the log, it just doesn’t send anything.
I would actually like to split one more level, since the payload without splitting is:
“Smokedetector bedroom - alarm”
So ideally I’d like to have only “bedroom” as the outcome of this split, but I don’t know how do do it. Can somebody please put me out of my misery?