Better (smaller) code for TAG Automation notifications

Hi, I use a TAG in an automation to disarm my Ring alarm. I want to notify 2 phones when the alarm has been disarmed and by using the tag attribute last_scanned_by_device_id: I want the message to include who disarmed the alarm. I have this working version of the notifications but I wonder if there is another way (maybe better code) to achieve this?

if:
  - condition: state
    entity_id: tag.fa060ebe_14f5_496c_8d2b_12we224ff
    attribute: last_scanned_by_device_id
    state: 9ffce8fcd6911a0071ffrjs52dd2ff5t55
then:
  - action: notify.mobile_app_john_phone
    metadata: {}
    data:
      message: Home Alarm Disarmed by john!
      title: Ring ALARM
      data:
        ttl: 0
        priority: high
  - action: notify.mobile_app_jane_phone
    metadata: {}
    data:
      data:
        ttl: 0
        priority: high
      message: Home Alarm Disarmed by john!
      title: Ring ALARM
else:
  - if:
      - condition: state
        entity_id: tag.fa060ebe_14f5_496c_8d2b_12we224ff
        attribute: last_scanned_by_device_id
        state: 0561ed946989e68f5d5e5525cd65frr2f
    then:
      - action: notify.mobile_app_john_phone
        metadata: {}
        data:
          message: Home Alarm Disarmed by jane!
          title: Ring ALARM
          data:
            ttl: 0
            priority: high
      - action: notify.mobile_app_jane_phone
        metadata: {}
        data:
          data:
            ttl: 0
            priority: high
          title: Ring ALARM
          message: Home Alarm Disarmed by jane!

Than you

Your code works, but it can be optimized for readability and maintainability. Here’s a better approach using a variable to determine who scanned the tag, reducing repetition:

trigger:
  - platform: state
    entity_id: tag.fa060ebe_14f5_496c_8d2b_12we224ff
    attribute: last_scanned_by_device_id
action:
  - variables:
      disarmer: >
        {% if trigger.to_state.attributes.last_scanned_by_device_id == "9ffce8fcd6911a0071ffrjs52dd2ff5t55" %}
          john
        {% elif trigger.to_state.attributes.last_scanned_by_device_id == "0561ed946989e68f5d5e5525cd65frr2f" %}
          jane
        {% else %}
          unknown
        {% endif %}
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ disarmer != 'unknown' }}"
        sequence:
          - service: notify.mobile_app_john_phone
            data:
              message: "Home Alarm Disarmed by {{ disarmer }}!"
              title: Ring ALARM
              data:
                ttl: 0
                priority: high
          - service: notify.mobile_app_jane_phone
            data:
              message: "Home Alarm Disarmed by {{ disarmer }}!"
              title: Ring ALARM
              data:
                ttl: 0
                priority: high
      - conditions:
          - condition: template
            value_template: "{{ disarmer == 'unknown' }}"
        sequence:
          - service: notify.mobile_app_john_phone
            data:
              message: "Home Alarm Disarmed by an unknown device!"
              title: Ring ALARM
              data:
                ttl: 0
                priority: high
          - service: notify.mobile_app_jane_phone
            data:
              message: "Home Alarm Disarmed by an unknown device!"
              title: Ring ALARM
              data:
                ttl: 0
                priority: high

  • Use of Variables: The disarmer variable determines who scanned the tag based on last_scanned_by_device_id, avoiding repetitive conditions.
  • Readability: The code is easier to read and maintain, as it avoids duplicating notification actions.
  • Fallback for Unknown Devices: The automation gracefully handles cases where the scanned device ID doesn’t match the known IDs.

Thank you! :slight_smile: Exactly what I was looking and hoping for. I had the whole concept in my mind but did not know how to make it reality.

@robert.access

One question though, it looks like the trigger only works if the tag is scanned alternately by phone # 1 then phone # 2 then phone #1 (or vice-versa) and so on… if I use Phone # 1 after I previously used phone # 1(or 2) for the second time, the trigger is not “triggered”?

can you try like this:

trigger:
  - platform: event
    event_type: tag_scanned
    event_data:
      tag_id: fa060ebe_14f5_496c_8d2b_12we224ff
action:
  - variables:
      disarmer: >
        {% if trigger.event.data.device_id == "9ffce8fcd6911a0071ffrjs52dd2ff5t55" %}
          john
        {% elif trigger.event.data.device_id == "0561ed946989e68f5d5e5525cd65frr2f" %}
          jane
        {% else %}
          unknown
        {% endif %}
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ disarmer != 'unknown' }}"
        sequence:
          - service: notify.mobile_app_john_phone
            data:
              message: "Home Alarm Disarmed by {{ disarmer }}!"
              title: Ring ALARM
              data:
                ttl: 0
                priority: high
          - service: notify.mobile_app_jane_phone
            data:
              message: "Home Alarm Disarmed by {{ disarmer }}!"
              title: Ring ALARM
              data:
                ttl: 0
                priority: high
      - conditions:
          - condition: template
            value_template: "{{ disarmer == 'unknown' }}"
        sequence:
          - service: notify.mobile_app_john_phone
            data:
              message: "Home Alarm Disarmed by an unknown device!"
              title: Ring ALARM
              data:
                ttl: 0
                priority: high
          - service: notify.mobile_app_jane_phone
            data:
              message: "Home Alarm Disarmed by an unknown device!"
              title: Ring ALARM
              data:
                ttl: 0
                priority: high

because the state platform in your trigger only activates when the state of the specified entity changes. If the tag is scanned repeatedly by the same phone, the state of the tag entity doesn’t change, and the trigger isn’t fired

Please let me know

Hi @robert.access ,

This one works. The only thing I had to do is use ’ instead of " on
“9ffce8fcd6911a0071ffrjs52dd2ff5t55” and “0561ed946989e68f5d5e5525cd65frr2f” as I was getting an error “expected token not integer”.

Thank you!

Joe68 you’re welcome,
Yaml can interpret strings with special characters as other data type. Try this:

disarmer: >
  {% if trigger.event.data.device_id == '9ffce8fcd6911a0071ffrjs52dd2ff5t55' %}
    john
  {% elif trigger.event.data.device_id == '0561ed946989e68f5d5e5525cd65frr2f' %}
    jane
  {% else %}
    unknown
  {% endif %}

difference is "......" to this '....'  double to single quote

Yea, that is what I did…thx

@robert.access

Could you please answer my question below?

  - choose:
      - conditions:
          - condition: template
            value_template: "{{ disarmer != 'unknown' }}"
        sequence:
          - service: notify.mobile_app_john_phone
            data:
              message: "Home Alarm Disarmed by {{ disarmer }}!"
              title: Ring ALARM
              data:
                ttl: 0
                priority: high
          - service: notify.mobile_app_jane_phone
            data:
              message: "Home Alarm Disarmed by {{ disarmer }}!"
              title: Ring ALARM
              data:
                ttl: 0
                priority: high
      - conditions:
          - condition: template
            value_template: "{{ disarmer == 'unknown' }}"
        sequence:
          - service: notify.mobile_app_john_phone
            data:
              message: "Home Alarm Disarmed by an unknown device!"
              title: Ring ALARM
              data:
                ttl: 0
                priority: high
          - service: notify.mobile_app_jane_phone
            data:
              message: "Home Alarm Disarmed by an unknown device!"
              title: Ring ALARM
              data:
                ttl: 0
                priority: high

How does it know when to send

message: "Home Alarm Disarmed by {{ disarmer }}!"

or

message: "Home Alarm Disarmed by an unknown device!"

Is it because the first one has a ! after disarmer?

value_template: "{{ disarmer != 'unknown' }}"

and the other one doesn’t?

value_template: "{{ disarmer = 'unknown' }}"

Does ! mean “false” and without means “true”?

Thank you again

!= …not equal to
== …equal to
disarmer == 'unknown'True when disarmer is exactly 'unknown'

make any sense?

when disarmer is not 'unknown', it sends the first message.
when disarmer is 'unknown', it sends the second message.

Thank you… love learning… I’m more of a VBA … this is all new to me