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.
I can help you do that (it’s easy) but I would much rather help you redesign this automation. In its current form, it triggers for each and every state-change that occurs in your system. That means every time any entity experiences a change in its state value, this automation is executed. It casts a very wide net to trap one desired fish.
Let me know if that interests you.
If not, you can just alter the payload_template (of this very busy automation) like this in order to extract
Thank you! Of course I would appreciate help for a redesign very much!
Edit: Oh, and the naming format doesn’t hold I’m afraid. Some rooms have names with several words, like “second bedroom upstairs”. The things that are the same are the first word (Smokedetector) and the space after that, and then the space and the dash and “alarm”.
Right. In LUA I would copy the sentence to memory and remove the prefix and the suffix with a simple search/replace, but I don’t know if that’s possible in YAML.
That seems to be an unrelated error message (it doesn’t even contain the template I suggested). It’s complaining about the fact that the trigger variable contains a null object (none) and a null object has no attributes so it can’t get device_class from something that contains nothing.
How did you try to test the automation? You can’t manually trigger it because that simply skips the automation’s trigger and condition; the trigger variable will be none.
I apologize, I didn’t actually test it because I thought the error meant that it didn’t work at all. I tested now, and it gave the desired result, thanks! So whenever you have time I’m ready for redesign.
OK, first step is to determine if you mind putting all of your smoke sensors in a group. That means if you ever install another smoke sensor, you will need to include it in the group in order for it to be monitored by the automation.
Your choice influences the template’s design. It will either monitor the group’s members or all sensor entities. How many are there? If there aren’t many then we can simply put them directly into a State Trigger and avoid using a group.
Actually I have never used grouping for anything. But I have nothing against grouping if that’s the easiest way to do it. I will put inn all the smoke alarms this weekend, so I probably won’t need to do anything more with it, at least not until one of the alarms fail x years from now. If it influences the decision, I am running this with ZWave JS 2 MQTT, and it is on a Pi 4, so processing power is not really an issue, I don’t think it minds checking all event changes.
Although this will look a bit verbose (because you must list all of the binary_sensors representing your 20-25 smoke detectors) it’s the easiest and most efficient way to get the job done. Should you need to ever do something else with all of the smoke sensors, then we can use a group. Until then, this is very straightforward.
Change the entity_ids to the names of all 20+ smoke detectors you wish to monitor.
- id: '1360646345812'
alias: Smoke alarm
description: Send event on MQTT when a smoke alarm is triggered
trigger:
- platform: state
entity_id:
- binary_sensor.smoke_alarm1
- binary_sensor.smoke_alarm2
- binary_sensor.smoke_alarm3
- binary_sensor.smoke_alarm25
from: 'off'
to: 'on'
action:
- service: mqtt.publish
data:
payload: "{{ trigger.to_state.attributes.friendly_name[14:-8] }}"
topic: eg/Smoke
EDIT
Correction. Fixed indentation (from and to). Replaced event_id with entity_id.
No, I’m afraid it didn’t work. Something happened, and it’s complaining about a line I didn’t change, the ID:
while parsing a block mapping in "/home/homeassistant/.homeassistant/automations.yaml", line 855, column 3 expected <block end>, but found '<block mapping start>' in "/home/homeassistant/.homeassistant/automations.yaml", line 865, column 4
Edit: I have changed it to Norweigan (I translated to English in the version I put up here, so it would be understandable), but I don’t think anything went wrong there. The Norwegian special characters are littered all around in my automations, and they have never given me trouble before. But this is the full, Norwegian automation:
#Sende beskjed når røykvarsleren blir utløst
- id: '1360646345812'
alias: Røykvarsling
description: Når en røykvarsler begynner å hyle, sendes event til EG
trigger:
- platform: state
event_id:
- binary_sensor.roykvarsler_soverom_sor_smoke_alarm_smoke_detected
- binary_sensor.smoke_alarm2
- binary_sensor.smoke_alarm3
- binary_sensor.smoke_alarm25
from: 'off'
to: 'on'
action:
- service: mqtt.publish
data:
payload: "{{ trigger.to_state.attributes.friendly_name[11:-8] }}"
topic: eg/Røykvarsler utløst
That might be due to an indentation error I made in my example. I have corrected it above. Indent from and to by one more space so that they are vertically aligned with platform. The other mistake is that I used event_id when it should be entity_id. Sorry about that.
Compare your version with the corrected example above (you will see the differences).
Glad to hear it works and also resolves the original problem of extracting the room’s name.
Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. It will also place a link below your first post that leads to the solution. All of this helps users find answers to similar questions. For more information refer to guideline 21 in the FAQ.