Variable friendly name of device in notification

I have 2 automations for getting a notification when my 2 Irobot’s there bin is full.
That is working fine.

But I want 1 automation for both of the Vacuum cleaners. But I want the friendly name of the Vacuum in the notification. So I know which one has a full bin.

This is my automation of one vacuum cleaner

alias: Notificatie Carlos opvangbak vol
description: Stuur een notificatie naar Yves & Bjorn om Carlos zijn zak leeg te maken.
trigger:
  - type: turned_on
    platform: device
    device_id: bd115620d58ace81db0ccaf876e8a38e
    entity_id: binary_sensor.carlos_bin_full
    domain: binary_sensor
condition: []
action:
  - service: notify.notify
    data:
      message: Carlos zijn opvangbak is vol
      title: Carlos
mode: single

In the Message and Title I want the name to be variable. I tried already several things but it’s not working. So how can I get the name of the Vacuum cleaner in the message as a variable?
Now there is only one trigger, but if variable is working the will be 2 triggers.
One for vacuum cleaner 1 and one for vacuum cleaner 2.

alias: Notificatie Carlos opvangbak vol
description: Stuur een notificatie naar Yves & Bjorn om Carlos zijn zak leeg te maken.
trigger:
  - platform: state
    entity_id:
      - binary_sensor.carlos_bin_full
      - binary_sensor.yves_bin_full
      - binary_sensor.bjorn_bin_full
    from: 'off'
    to: 'on'
condition: []
action:
  - service: notify.notify
    data:
      message: "{{ trigger.to_state.name }} is vol"
      title:  "{{ trigger.to_state.name }}"
mode: single

Reference: Automation Trigger Variables - State Trigger

2 Likes

Almost working but name not yet as I want it.
The sensor’s name is “Carlos Bin Full” … The vacuum itselfs name is Carlos.

Now I get the notifications ‘Carlos Bin Full’ is vol.
And it should be ‘Carlos’ is vol.

So how can I get the name of the vacuum cleaner (entity-id = vacuum.carlos)
and the other one is vacuum.cleopatra

Home Assistant only knows the binary_sensor’s data, not other data like what you called the vacuum.

If all of the binary_sensor’s are in this format:

binary_sensor.vacuumname_bin_full

then we can extract the “vacuumname” portion like this:

alias: Notificatie Carlos opvangbak vol
description: Stuur een notificatie naar Yves & Bjorn om Carlos zijn zak leeg te maken.
trigger:
  - platform: state
    entity_id:
      - binary_sensor.carlos_bin_full
      - binary_sensor.yves_bin_full
      - binary_sensor.bjorn_bin_full
    from: 'off'
    to: 'on'
condition: []
action:
  - variables:
      name: "{{ trigger.to_state.object_id.split('_')[0] | title }}"
  - service: notify.notify
    data:
      message: "{{ name }} is vol"
      title:  "{{ name }}"
mode: single

So if binary_sensor.bjorn_bin_full is responsible for triggering the State Trigger, the value of trigger.to_state.object_id will be bjorn_bin_full.

If we split it at every underscore character it will produce a list like this ['bjorn', 'bin', 'full'] but we only want the first item in the list so we reference it using [0] because lists are zero-indexed. The result is bjorn.

Finally, we capitalize the first letter using the title filter to get Bjorn.

Wow thanks for not only providing the code but also the detailed explanation.
This really helps me to understand the coding.

Thank mate!

1 Like

To reduce my automations I want 3 automations in 1 variable automations.

There are 3 devices that I want to send a notification when they have ended there cyclys.

This is my automation:

description: >-
  Stuur een melding naar Yves & Bjorn wanneer een toestel het ingestelde
  programma heeft voltooid
mode: single
trigger:
  - platform: numeric_state
    entity_id: sensor.stopcontact_vaatwasser_power_2
    for:
      hours: 0
      minutes: 2
      seconds: 0
    below: 1
  - platform: numeric_state
    entity_id: sensor.stopcontact_wasmachine_power_2
    for:
      hours: 0
      minutes: 2
      seconds: 0
    below: 1
  - platform: numeric_state
    entity_id: sensor.stopcontact_droogkast_power_2
    for:
      hours: 0
      minutes: 2
      seconds: 0
    below: 1
variables:
  name: "{{ trigger.to_numeric_state.object_id.split('_')[0] }}"
condition:
  - condition: state
    entity_id: input_boolean.{{ name }}
    state: "on"
action:
  - service: input_boolean.turn_off
    data: {}
    target:
      entity_id: input_boolean.{{ name }}
  - service: notify.yves
    data:
      message: >-
        De {{ name }} is klaar met het ingestelde programma, {{ name }} leeg
        maken aub.
      title: {{ name }}
  - service: tts.google_translate_say
    data:
      entity_id:
        - media_player.living_2
        - media_player.keuken_2
        - media_player.badkamer_2
        - media_player.slaapkamer_2
      message: >-
        De {{ name }} is klaar met het ingestelde programma, {{ name }} leeg maken aub.
    enabled: false

But I cannot save the automation because of this error: Message malformed: Entity input_boolean.{{ name }} is neither a valid entity ID nor a valid UUID for dictionary value @ data[‘condition’][0][‘entity_id’]

So the variable for the condition rule it not recognized.
Any one that can help?

If anyone has the same issue. I have used other method. So this is my automation now.
1 automation to check 3 devices if they have started or ended their cyclys.

alias: Notificatie wanneer een toestel gestart of klaar is met programma
description: >-
  Stuur een melding naar Yves & Bjorn dat een toestel gestart of klaar is met
  het ingestelde programma.
trigger:
  - type: power
    platform: device
    device_id: 8bd03b38ad09d6ffaa53d0be1b47447a
    entity_id: sensor.stopcontact_vaatwasser_power_2
    domain: sensor
    above: 25
    for:
      hours: 0
      minutes: 0
      seconds: 0
    id: Vaatwasser gestart
  - type: power
    platform: device
    device_id: 8bd03b38ad09d6ffaa53d0be1b47447a
    entity_id: sensor.stopcontact_vaatwasser_power_2
    domain: sensor
    below: 1
    for:
      hours: 0
      minutes: 5
      seconds: 0
    id: Vaatwasser klaar
  - type: power
    platform: device
    device_id: 5d01809d129d66d4affd1b72ef550b3e
    entity_id: sensor.stopcontact_wasmachine_power_2
    domain: sensor
    above: 25
    id: Wasmachine gestart
  - type: power
    platform: device
    device_id: 5d01809d129d66d4affd1b72ef550b3e
    entity_id: sensor.stopcontact_wasmachine_power_2
    domain: sensor
    below: 1
    for:
      hours: 0
      minutes: 5
      seconds: 0
    id: Wasmachine klaar
  - type: power
    platform: device
    device_id: be4ff233254e252000cbe35b39a42ab6
    entity_id: sensor.stopcontact_droogkast_power_2
    domain: sensor
    above: 25
    id: Droogkast gestart
  - type: power
    platform: device
    device_id: be4ff233254e252000cbe35b39a42ab6
    entity_id: sensor.stopcontact_droogkast_power_2
    domain: sensor
    for:
      hours: 0
      minutes: 5
      seconds: 0
    below: 1
    id: Droogkast klaar
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: Vaatwasser gestart
          - condition: state
            entity_id: input_boolean.vaatwasser
            state: "off"
        sequence:
          - service: input_boolean.turn_on
            data: {}
            target:
              entity_id: input_boolean.vaatwasser
          - service: notify.yves
            data:
              message: Vaatwasser is gestart met het ingesteld programma
              title: Vaatwasser
      - conditions:
          - condition: trigger
            id: Vaatwasser klaar
          - condition: state
            entity_id: input_boolean.vaatwasser
            state: "on"
        sequence:
          - service: input_boolean.turn_off
            data: {}
            target:
              entity_id: input_boolean.vaatwasser
          - service: notify.yves
            data:
              message: Vaatwasser is klaar en kan worden uitgeladen.
              title: Vaatwasser
          - service: tts.google_translate_say
            data:
              entity_id:
                - media_player.living_2
                - media_player.keuken_2
                - media_player.badkamer_2
                - media_player.slaapkamer_2
              message: >-
                De vaatwasser is klaar met het ingestelde programma en kan
                worden uitgeladen.
      - conditions:
          - condition: trigger
            id: Wasmachine gestart
          - condition: state
            entity_id: input_boolean.wasmachine
            state: "off"
        sequence:
          - service: input_boolean.turn_on
            data: {}
            target:
              entity_id: input_boolean.wasmachine
          - service: notify.yves
            data:
              message: Wasmachine is gestart met het ingesteld programma
              title: Wasmachine
      - conditions:
          - condition: trigger
            id: Wasmachine klaar
          - condition: state
            entity_id: input_boolean.wasmachine
            state: "on"
        sequence:
          - service: input_boolean.turn_off
            data: {}
            target:
              entity_id: input_boolean.wasmachine
          - service: notify.yves
            data:
              message: Wasmachine is klaar en kan worden uitgeladen.
              title: Wasmachine
          - service: tts.google_translate_say
            data:
              entity_id:
                - media_player.living_2
                - media_player.keuken_2
                - media_player.badkamer_2
                - media_player.slaapkamer_2
              message: >-
                De wasmachine is klaar met het ingestelde programma en kan
                worden uitgeladen.
      - conditions:
          - condition: trigger
            id: Droogkast gestart
          - condition: state
            entity_id: input_boolean.droogkast
            state: "off"
        sequence:
          - service: input_boolean.turn_on
            data: {}
            target:
              entity_id: input_boolean.droogkast
          - service: notify.yves
            data:
              message: Droogkast is gestart met het ingesteld programma
              title: Droogkast
      - conditions:
          - condition: trigger
            id: Droogkast klaar
          - condition: state
            entity_id: input_boolean.droogkast
            state: "on"
        sequence:
          - service: input_boolean.turn_off
            data: {}
            target:
              entity_id: input_boolean.droogkast
          - service: notify.yves
            data:
              message: Droogkast is klaar en kan worden uitgeladen.
              title: Droogkast
          - service: tts.google_translate_say
            data:
              entity_id:
                - media_player.living_2
                - media_player.keuken_2
                - media_player.badkamer_2
                - media_player.slaapkamer_2
              message: >-
                De droogkast is klaar met het ingestelde programma en kan worden
                uitgeladen.
mode: queued

For future reference, the trigger variable doesn’t have a to_numeric_state property. As a result, your template for name produces a null value which causes the error message wherever name is referenced.

In addition, it contains syntax errors like this one:

      title: {{ name }}

The template lacks outer quotes. If a template is on the same line as the option, it must be quoted.

      title: "{{ name }}"

Reference: Important Templating Rules

I am doing ping test of network devices and can successfully pass the name of the device that went offline with the “Device “{{ trigger.to_state.attributes.friendly_name }}” is offline”. I want to do another test every 30 minutes after failure to tell me which devices remain offline with a numeric value of 0 but I can’t see how to pass the friendly name in this test because none have had a state change. For it to pass the test, it has to already be at 0. How can I pass along the friendly name for the device that remains at 0 and has not had a state change?