How do I split friendly name correctly in this automation?

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?

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

bedroom

from:

Smokedetector bedroom - alarm
      payload_template: "{{ state_attr(trigger.event.data.entity_id, 'friendly_name').split()[1] }}"

This assumes all of your smoke sensors follow the same naming format (second word in friendly_name is the room name).

Thank you! Of course I would appreciate help for a redesign very much! :grinning_face_with_smiling_eyes:

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”.

OK but, for now, make the suggested alteration and check it produces the desired result. If it fails, let’s fix that first.

Oops, just saw that the naming format isn’t consistent …

Try this:

      payload_template: "{{ state_attr(trigger.event.data.entity_id, 'friendly_name')[14:-8] }}"

It eliminates the first 14 and the last 8 characters then reports whatever remains.

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.

Ah, we both do our edits here… :wink: Anyway, no. It didn’t work, I’m afraid:

Template variable error: 'None' has no attribute 'attributes' when rendering '{{ trigger.event.data.new_state.attributes.device_class == "smoke" }}'

And this was while not even running a smoke alarm.

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! :smiley: So whenever you have time I’m ready for redesign.

Put your extracted attribute in parentheses and add .replace(“find_string”,“new_string”)

Like (state_attr(trigger.event.data.entity_id, ‘friendly_name’)).replace(" - alarm","")

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.

And there will be around 20-25 of them.

That’s a few more than the 4 or 5 I had assumed. :slightly_smiling_face:

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. :man_facepalming:

Thanks! I’ll do that, then. :+1:

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).

Thanks a lot again! It works, and no errors this time! :grin:

1 Like

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.

It’s hereby done! :slight_smile:

1 Like