NFC tag : how to check who scanned it

choose:
  - conditions:
      - condition: template
        value_template: |2-
                        {% set person =
                        states.person|selectattr('attributes.user_id','eq',trigger.event.context.user_id)|first%}
                        {{ 'Joachim' in person.attributes.friendly_name }}
    sequence:
      - service: notify.mobile_app_gsm_joachim
        data:
          title: test
          message: Joachim optie1 heeft de tag in ruimte 1 gescanned

does not work for me

my code was wrong, this is ok. It checks the tag, not the person

afbeelding

I think it’s related to this bug, Context user_id ist not propagated when an automation is triggered by a MobileApp event. · Issue #90669 · home-assistant/core · GitHub. Also the example code only work when I manually triggered it, it did not work from the mobile device.

Please post the dowmloaded trace, I’m not familiar with Dutch, so I (and others) can’t read what’s happening there…

ha_240629_automation_trace_download

It’s still unclear what you want and what doesn’t work, my example works 100%, what do you need to check?

the tag id ??
You still don’t understand that activating a condition is not formally correct.

what is the difference between:
trigger->event->condition->tag_id

or

trigger->tag_id

???

1 Like

hi @Faecon,

could you please try the below. please check to make sure the user_id’s are correct for the proper id’s in your system.

alias: test
description: ""
trigger:
  - platform: event
    event_type: tag_scanned
    context: {}
    event_data: {}
condition: []
action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ trigger.event.context.user_id == '5fa8377fe546493fb4c541aac336d2c6' }}"
        sequence:
          - service: notify.mobile_app_gsm_joachim
            data:
              title: test
              message: >-
                5fa8377fe546493fb4c541aac336d2c6 heeft de tag in ruimte 1
                gescanned
      - conditions:
          - condition: template
            value_template: "{{ trigger.event.context.user_id == '63ae2011ba54413ea0df17a78f6b0f7a' }}"
            enabled: true
        sequence:
          - service: notify.mobile_app_gsm_joachim
            data:
              title: test
              message: joachim heeft de tag in ruimte 1 gescanned
      - conditions:
          - condition: template
            value_template: "{{ trigger.event.context.user_id == '7ed612d37b704e1cb33004322b946082' }}"
            enabled: true
        sequence:
          - service: notify.mobile_app_gsm_joachim
            data:
              title: test
              message: joachim heeft de tag in ruimte 1 gescanned
mode: single

What I want:

I have one tag. If scanned I want the light on, the temperature of the HVAC on and the music from the sonos playing.
I have 6 people who can scan that tag. So 6 different adjustments possible (person a want it 21 degrees, warm light and radio 1 playing, person b wants it 20 degrees, white light and radio2 playing).

I have 6 rooms, so I want in each room a tag where does adjustments can me made

{
  "trace": {
    "last_step": "action/0/choose/3/conditions/0",
    "run_id": "4acd656a71e06d8a90f29cd4ff661400",
    "state": "stopped",
    "script_execution": "finished",
    "timestamp": {
      "start": "2024-06-29T16:51:36.682519+00:00",
      "finish": "2024-06-29T16:51:36.684796+00:00"
    },
    "domain": "automation",
    "item_id": "1719678733013",
    "trigger": "event 'tag_scanned'",
    "trace": {
      "trigger/0": [
        {
          "path": "trigger/0",
          "timestamp": "2024-06-29T16:51:36.682562+00:00",
          "changed_variables": {
            "this": {
              "entity_id": "automation.test",
              "state": "on",
              "attributes": {
                "id": "1719678733013",
                "last_triggered": "2024-06-29T16:48:12.654707+00:00",
                "mode": "single",
                "current": 0,
                "friendly_name": "test",
                "initial_state": true
              },
              "last_changed": "2024-06-29T16:51:29.079469+00:00",
              "last_reported": "2024-06-29T16:51:29.079469+00:00",
              "last_updated": "2024-06-29T16:51:29.079469+00:00",
              "context": {
                "id": "01J1JEHBNQAHTY596BD8E6H351",
                "parent_id": null,
                "user_id": null
              }
            },
            "trigger": {
              "id": "0",
              "idx": "0",
              "alias": null,
              "platform": "event",
              "event": {
                "event_type": "tag_scanned",
                "data": {
                  "tag_id": "0b3128d7-a0fc-4f56-a7fa-ad6041ea207c",
                  "name": "Tag 0b3128d7-a0fc-4f56-a7fa-ad6041ea207c",
                  "device_id": "3f55d1248acfc2ad51546a2556124590"
                },
                "origin": "LOCAL",
                "time_fired": "2024-06-29T16:51:36.682359+00:00",
                "context": {
                  "id": "01J1JEHK3AF92YRBG9GXFZQA2N",
                  "parent_id": null,
                  "user_id": "5fa8377fe546493fb4c541aac336d2c6"
                }
              },
              "description": "event 'tag_scanned'"
            }
          }
        }
      ],
      "condition/0": [
        {
          "path": "condition/0",
          "timestamp": "2024-06-29T16:51:36.682582+00:00",
          "result": {
            "result": true,
            "entities": []
          }
        }
      ],
      "action/0": [
        {
          "path": "action/0",
          "timestamp": "2024-06-29T16:51:36.682896+00:00",
          "changed_variables": {
            "context": {
              "id": "01J1JEHK3AWRAA84CQ4Y599FPG",
              "parent_id": "01J1JEHK3AF92YRBG9GXFZQA2N",
              "user_id": null
            }
          }
        }
      ],
      "action/0/choose/0": [
        {
          "path": "action/0/choose/0",
          "timestamp": "2024-06-29T16:51:36.683073+00:00",
          "result": {
            "result": null
          }
        }
      ],
      "action/0/choose/0/conditions/0": [
        {
          "path": "action/0/choose/0/conditions/0",
          "timestamp": "2024-06-29T16:51:36.683091+00:00",
          "error": "In 'template' condition: UndefinedError: No first item, sequence was empty."
        }
      ],
      "action/0/choose/1": [
        {
          "path": "action/0/choose/1",
          "timestamp": "2024-06-29T16:51:36.684201+00:00",
          "result": {
            "result": false
          }
        }
      ],
      "action/0/choose/1/conditions/0": [
        {
          "path": "action/0/choose/1/conditions/0",
          "timestamp": "2024-06-29T16:51:36.684218+00:00",
          "result": {
            "result": false,
            "entities": []
          }
        }
      ],
      "action/0/choose/2": [
        {
          "path": "action/0/choose/2",
          "timestamp": "2024-06-29T16:51:36.684290+00:00",
          "result": {
            "result": false
          }
        }
      ],
      "action/0/choose/2/conditions/0": [
        {
          "path": "action/0/choose/2/conditions/0",
          "timestamp": "2024-06-29T16:51:36.684303+00:00",
          "result": {
            "result": false,
            "entities": []
          }
        }
      ],
      "action/0/choose/3": [
        {
          "path": "action/0/choose/3",
          "timestamp": "2024-06-29T16:51:36.684381+00:00",
          "result": {
            "result": null
          }
        }
      ],
      "action/0/choose/3/conditions/0": [
        {
          "path": "action/0/choose/3/conditions/0",
          "timestamp": "2024-06-29T16:51:36.684394+00:00",
          "error": "In 'template' condition: UndefinedError: No first item, sequence was empty."
        }
      ]
    },
    "config": {
      "id": "1719678733013",
      "alias": "test",
      "description": "",
      "trigger": [
        {
          "platform": "event",
          "event_type": "tag_scanned",
          "context": {},
          "event_data": {}
        }
      ],
      "condition": [
        {
          "condition": "template",
          "value_template": "{{ trigger.event.data.tag_id == '0b3128d7-a0fc-4f56-a7fa-ad6041ea207c'}} "
        }
      ],
      "action": [
        {
          "choose": [
            {
              "conditions": [
                {
                  "condition": "template",
                  "value_template": "{% set person =\nstates.person|selectattr('attributes.user_id','eq',trigger.event.context.user_id)|first%}\n{{ 'Joachim' in person.attributes.friendly_name }}"
                }
              ],
              "sequence": [
                {
                  "service": "notify.mobile_app_gsm_joachim",
                  "data": {
                    "title": "test",
                    "message": "Joachim optie1 heeft de tag in ruimte 1 gescanned"
                  }
                }
              ]
            },
            {
              "conditions": [
                {
                  "condition": "template",
                  "value_template": "{{ this.context.user_id == 'joachim' }}",
                  "enabled": true
                }
              ],
              "sequence": [
                {
                  "service": "notify.mobile_app_gsm_joachim",
                  "data": {
                    "title": "test",
                    "message": "joachim heeft de tag in ruimte 1 gescanned"
                  }
                }
              ]
            },
            {
              "conditions": [
                {
                  "condition": "template",
                  "value_template": "{{ this.context.user_id == 'Joachim' }}",
                  "enabled": true
                }
              ],
              "sequence": [
                {
                  "service": "notify.mobile_app_gsm_joachim",
                  "data": {
                    "title": "test",
                    "message": "joachim heeft de tag in ruimte 1 gescanned"
                  }
                }
              ]
            },
            {
              "conditions": [
                {
                  "condition": "template",
                  "value_template": "              {% set person =\n              states.person|selectattr('attributes.user_id','eq',trigger.event.context.user_id)|first%}\n              {{ 'joachim' in person.attributes.friendly_name }}",
                  "enabled": true
                }
              ],
              "sequence": [
                {
                  "service": "notify.mobile_app_gsm_joachim",
                  "data": {
                    "title": "test",
                    "message": "joachim (optie4) heeft de tag in ruimte 1 gescanned"
                  }
                }
              ]
            }
          ]
        }
      ],
      "mode": "single"
    },
    "blueprint_inputs": null,
    "context": {
      "id": "01J1JEHK3AWRAA84CQ4Y599FPG",
      "parent_id": "01J1JEHK3AF92YRBG9GXFZQA2N",
      "user_id": null
    }
  },
  "logbookEntries": [
    {
      "name": "test",
      "message": "triggered by event 'tag_scanned'",
      "source": "event 'tag_scanned'",
      "entity_id": "automation.test",
      "context_id": "01J1JEHK3AWRAA84CQ4Y599FPG",
      "when": 1719679896.6827264,
      "domain": "automation"
    }
  ]
}

afbeelding

that runs the action. But I did not get a message on my mobile

{
  "trace": {
    "last_step": "action/0/choose/0/sequence/0",
    "run_id": "17e6cd89458a30744a8e0418d28baefe",
    "state": "stopped",
    "script_execution": "finished",
    "timestamp": {
      "start": "2024-06-30T04:48:12.148210+00:00",
      "finish": "2024-06-30T04:48:12.657313+00:00"
    },
    "domain": "automation",
    "item_id": "1719678733013",
    "trigger": "event 'tag_scanned'",
    "trace": {
      "trigger/0": [
        {
          "path": "trigger/0",
          "timestamp": "2024-06-30T04:48:12.148268+00:00",
          "changed_variables": {
            "this": {
              "entity_id": "automation.test",
              "state": "on",
              "attributes": {
                "id": "1719678733013",
                "last_triggered": "2024-06-30T04:47:00.311001+00:00",
                "mode": "single",
                "current": 0,
                "friendly_name": "test",
                "initial_state": true
              },
              "last_changed": "2024-06-30T04:46:50.923001+00:00",
              "last_reported": "2024-06-30T04:47:00.879730+00:00",
              "last_updated": "2024-06-30T04:47:00.879730+00:00",
              "context": {
                "id": "01J1KQFGPPV4XXMXA4587TDRNG",
                "parent_id": "01J1KQFGPP7N9BK6ATBKS5765C",
                "user_id": null
              }
            },
            "trigger": {
              "id": "0",
              "idx": "0",
              "alias": null,
              "platform": "event",
              "event": {
                "event_type": "tag_scanned",
                "data": {
                  "tag_id": "0b3128d7-a0fc-4f56-a7fa-ad6041ea207c",
                  "name": "Tag 0b3128d7-a0fc-4f56-a7fa-ad6041ea207c",
                  "device_id": "3f55d1248acfc2ad51546a2556124590"
                },
                "origin": "LOCAL",
                "time_fired": "2024-06-30T04:48:12.148166+00:00",
                "context": {
                  "id": "01J1KQHPVMT6DPPJYZE5FANYTZ",
                  "parent_id": null,
                  "user_id": "5fa8377fe546493fb4c541aac336d2c6"
                }
              },
              "description": "event 'tag_scanned'"
            }
          }
        }
      ],
      "action/0": [
        {
          "path": "action/0",
          "timestamp": "2024-06-30T04:48:12.148466+00:00",
          "changed_variables": {
            "context": {
              "id": "01J1KQHPVM6WQXD9BQ5RQEDQ87",
              "parent_id": "01J1KQHPVMT6DPPJYZE5FANYTZ",
              "user_id": null
            }
          },
          "result": {
            "choice": 0
          }
        }
      ],
      "action/0/choose/0": [
        {
          "path": "action/0/choose/0",
          "timestamp": "2024-06-30T04:48:12.148522+00:00",
          "result": {
            "result": true
          }
        }
      ],
      "action/0/choose/0/conditions/0": [
        {
          "path": "action/0/choose/0/conditions/0",
          "timestamp": "2024-06-30T04:48:12.148540+00:00",
          "result": {
            "result": true,
            "entities": []
          }
        }
      ],
      "action/0/choose/0/sequence/0": [
        {
          "path": "action/0/choose/0/sequence/0",
          "timestamp": "2024-06-30T04:48:12.148688+00:00",
          "result": {
            "params": {
              "domain": "notify",
              "service": "mobile_app_m2012k11ag",
              "service_data": {
                "title": "test",
                "message": "Optie1 is gescanned"
              },
              "target": {}
            },
            "running_script": false
          }
        }
      ]
    },
    "config": {
      "id": "1719678733013",
      "alias": "test",
      "description": "",
      "trigger": [
        {
          "platform": "event",
          "event_type": "tag_scanned",
          "context": {},
          "event_data": {}
        }
      ],
      "condition": [],
      "action": [
        {
          "choose": [
            {
              "conditions": [
                {
                  "condition": "template",
                  "value_template": "{{ trigger.event.context.user_id == '5fa8377fe546493fb4c541aac336d2c6' }}"
                }
              ],
              "sequence": [
                {
                  "service": "notify.mobile_app_m2012k11ag",
                  "data": {
                    "title": "test",
                    "message": "Optie1 is gescanned"
                  }
                }
              ]
            },
            {
              "conditions": [
                {
                  "condition": "template",
                  "value_template": "{{ trigger.event.context.user_id == '63ae2011ba54413ea0df17a78f6b0f7a' }}",
                  "enabled": true
                }
              ],
              "sequence": [
                {
                  "service": "notify.mobile_app_gsm_joachim",
                  "data": {
                    "title": "test",
                    "message": "Optie2 is gescanned"
                  }
                }
              ]
            },
            {
              "conditions": [
                {
                  "condition": "template",
                  "value_template": "{{ trigger.event.context.user_id == '7ed612d37b704e1cb33004322b946082' }}",
                  "enabled": true
                }
              ],
              "sequence": [
                {
                  "service": "notify.mobile_app_gsm_joachim",
                  "data": {
                    "title": "test",
                    "message": "Optie3 is gescanned"
                  }
                }
              ]
            }
          ]
        }
      ],
      "mode": "single"
    },
    "blueprint_inputs": null,
    "context": {
      "id": "01J1KQHPVM6WQXD9BQ5RQEDQ87",
      "parent_id": "01J1KQHPVMT6DPPJYZE5FANYTZ",
      "user_id": null
    }
  },
  "logbookEntries": [
    {
      "name": "test",
      "message": "triggered by event 'tag_scanned'",
      "source": "event 'tag_scanned'",
      "entity_id": "automation.test",
      "context_id": "01J1KQHPVM6WQXD9BQ5RQEDQ87",
      "when": 1719722892.148303,
      "domain": "automation"
    }
  ]
}

Hi all,

this is working :

thanks all

alias: Ruimte1_nfctag
description: ""
trigger:
  - platform: event
    event_type: tag_scanned
    context: {}
    event_data: {}
condition:
  - condition: template
    value_template: "{{ trigger.event.data.tag_id == '0b3128d7-a0fc-4f56-a7fa-ad6041ea207c'}}"
action:
  - choose:
      - conditions:
          - condition: template
            value_template: >-
              {{ trigger.event.context.user_id ==
              '63ae2011ba54413ea0df17a78f6b0f7a' }}
        sequence: []
      - conditions:
          - condition: template
            value_template: >
              {{ trigger.event.context.user_id ==
              '7ed612d37b704e1cb33004322b946082' }}
        sequence: []
      - conditions:
          - condition: template
            value_template: >
              {{ trigger.event.context.user_id ==
              '5fa8377fe546493fb4c541aac336d2c6' }}
        sequence: []
      - conditions:
          - condition: template
            value_template: >
              {{ trigger.event.context.user_id ==
              '6fb794515d81474fb83d471ae15eaee9' }}
        sequence: []
      - conditions:
          - condition: template
            value_template: >
              {{ trigger.event.context.user_id ==
              'a743839fbddd4dbea2a81dfad0e949af' }}
        sequence: []
      - conditions:
          - condition: template
            value_template: >
              {{ trigger.event.context.user_id ==
              '9ef0afd606a348bb90874d135161b48d' }}
        sequence: []
mode: single

I’m still puzzled why you use event trigger with condition instead of trigger tag, mystery

What is the difference ? If your’s is better i will change it

I already told you in the example:

trigger:
  - platform: tag
    tag_id: 0b3128d7-a0fc-4f56-a7fa-ad6041ea207c
condition: []

your

trigger:
  - platform: event
    event_type: tag_scanned
    context: {}
    event_data: {}
condition:
  - condition: template
    value_template: "{{ trigger.event.data.tag_id == '0b3128d7-a0fc-4f56-a7fa-ad6041ea207c'}}"

it does not make sense

1 Like

In all honesty, posting a piece of code without explanation sometimes doesn’t get through to people. Some words would have gone a long way, for the OP, all others in this topic and you. I don’t see it as rude, but if you want your answer to be taken seriously, especially by beginners, you should give some more info, than just the (valid) code. :slight_smile:

I take it, it’s working for you now, right? :slight_smile: Good you got it working! :+1:

However, the change @sdavides proposed is a good idea, and you should use it! :wink: :slight_smile: I haven’t noticed it in his code either
What you porbably should think about, as you said you have different tags around the house, combining this into one automation for all of them. If you want to do this, let us know!

But I can see, that this was tedious to get to something, so I would perfectly understand, if you just want to leave it “as-is”, at least for now. :laughing:

2 Likes

Pffff what’s this about…

1 Like

sounds like you’re all set? holler if not…

i think the last issue you raised about not getting the notification was not about the condition check but was about the notification call.

regarding the hub-bub about trigger + condition vs having the trigger include the tag_id. they are functionally equivalent to each other… so don’t sweat it. combining it together in the trigger is a bit more elegant so you get more code beauty points :slight_smile: , and i think the system is able to do that a micro more efficiently. but again, functionally equivalent.

if it’s all working as you’d like it, then cheers!!!

1 Like

In fact I don’t need the notification, It was just for testing.
The scanning just need to do some actions :wink:

thanks

Is not equivalent

I made a blueprint for that