Get friendly_name of entity in condition

Hi all,
I’m trying to make an automation for my trash. I have an automation which looks everyday at 19:00:00 o’clock if a binary sensor is on. When the sensor is on, i send a message to my phone. Now I want to know which of the binary sensors from my conidtion was the one who make the automation start. Here is my code:

  - alias: 'Notify Trash Black'
    trigger:
      platform: time
      at: "19:00:00"
    condition:
       condition: or
       conditions:
         - condition: state
           entity_id: binary_sensor.morgen_schwarze_tonne
           state: 'on'
         - condition: state
           entity_id: binary_sensor.morgen_blaue_tonne
           state: 'on'
         - condition: state
           entity_id: binary_sensor.morgen_gruene_tonne
           state: 'on'
         - condition: state
           entity_id: binary_sensor.morgen_gelbe_tonne
           state: 'on'
    action:
      - service: counter.increment
        entity_id: counter.iosbadgecount_tim
      - service: counter.increment
        entity_id: counter.iosbadgecount_vio
      - service: notify.ios_tims_devices
        data_template:
          title: "Morgen wird Müll abgeholt"
          message: "INSERT FRIENDLY NAME OF BINARY SENSOR HERE"
          data:
           push:
            badge: '{{states.counter.iosbadgecount_tim.state}}'
            thread-id: "Muell"

Can anyone help me with this?

I’m sure you can do this with if statements, someone else can take the liberty of posting that. Here’s an expandable template but it uses ‘high level’ templating. I suggest you read up on templates and jinja and you could probably come up with an if - elif - else template on your own.

  - alias: 'Notify Trash Black'
    trigger:
      platform: time
      at: "19:00:00"
    condition:
       condition: or
       conditions:
         - condition: state
           entity_id: binary_sensor.morgen_schwarze_tonne
           state: 'on'
         - condition: state
           entity_id: binary_sensor.morgen_blaue_tonne
           state: 'on'
         - condition: state
           entity_id: binary_sensor.morgen_gruene_tonne
           state: 'on'
         - condition: state
           entity_id: binary_sensor.morgen_gelbe_tonne
           state: 'on'
    action:
      - service: counter.increment
        entity_id: counter.iosbadgecount_tim
      - service: counter.increment
        entity_id: counter.iosbadgecount_vio
      - service: notify.ios_tims_devices
        data_template:
          title: "Morgen wird Müll abgeholt"
          message: >
            {% set sensors = [
              states.binary_sensor.morgen_schwarze_tonne,
              states.binary_sensor.morgen_blaue_tonne,
              states.binary_sensor.morgen_gruene_tonne,
              states.binary_sensor.morgen_gelbe_tonne ] %}
            {{ sensors | selectattr('state', 'eq', 'on') | map(attribute='name') | list | first }}
          data:
           push:
            badge: '{{states.counter.iosbadgecount_tim.state}}'
            thread-id: "Muell"
1 Like

Templategod @petro :wink:

Thanks for your quick answer!
I think I have found the if solution:

  - alias: 'Notify Trash Black'
    trigger:
      platform: time
      at: "19:00:00"
    condition:
       condition: or
       conditions:
         - condition: state
           entity_id: binary_sensor.morgen_schwarze_tonne
           state: 'on'
         - condition: state
           entity_id: binary_sensor.morgen_blaue_tonne
           state: 'on'
         - condition: state
           entity_id: binary_sensor.morgen_gruene_tonne
           state: 'on'
         - condition: state
           entity_id: binary_sensor.morgen_gelbe_tonne
           state: 'on'
    action:
      - service: counter.increment
        entity_id: counter.iosbadgecount_tim
      - service: counter.increment
        entity_id: counter.iosbadgecount_vio
      - service: notify.ios_tims_devices
        data_template:
          title: "Morgen wird Müll abgeholt"
          message: "{% if is_state('binary_sensor.morgen_schwarze_tonne', 'on') %}
  Bitte die schwarze Tonne rausstellen!
{% elif is_state('binary_sensor.morgen_gruene_tonne', 'on') %}
  Bitte die grüne Tonne rausstellen!
{% elif is_state('binary_sensor.morgen_blaue_tonne', 'on') %}
  Bitte die blaue Tonne rausstellen!
{% elif is_state('binary_sensor.morgen_gelbe_tonne', 'on') %}
  Bitte die gelbe Tonne rausstellen! {% endif %}" 
          data:
           push:
            badge: '{{states.counter.iosbadgecount_tim.state}}'
            thread-id: "Muell"
      - service: notify.ios_vios_devices
        data_template:
          title: "Morgen wird Müll abgeholt"
          message: "Bitte die {{ trigger.to_state.attributes.friendly_name }}"
          data:
           push:
            badge: '{{states.counter.iosbadgecount_vio.state}}'
            thread-id: "Muell"
      - service: persistent_notification.create
        data:
          title: "Morgen wird Müll abgeholt"
          message: "Bitte die Tonne füllen"
          notification_id: muell_schwarz

Thanks for your answer. You were right, the documentation helps me a lot!

Wouldn’t it be easier to use trigger.entity_id here?

he’s using a time trigger, no entity. He’d have to change his automation completely, might get undesired results

Oh right, I see it now, I somehow assumed he was using the binary sensors as the trigger, my bad.