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