NFC tag : how to check who scanned it

the trigger condition a problem. I’m not asking about the choose conditions. Why is that there? the trigger condition in an automation is checked and enforced. So this condition you have means it will only allow that one person. If the user_id is not that one, the automation stops.

get rid of the trigger condition… keep the choose conditions

alias: Ruimte1_nfctag
description: ""
trigger:
  - platform: event
    event_type: tag_scanned
    context: {}
    event_data: {}
condition: []
action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ this.context.user_id == '63ae2011ba54413ea0df17a78f6b0f7a' }}"
        sequence:
          - service: notify.mobile_app_gsm_x
            data:
              title: test
              message: x heeft de tag in ruimte 1 gescanned
      - conditions:
          - condition: template
            value_template: >-
              {{ trigger.context.user_id == '7ed612d37b704e1cb33004322b946082'
              }}
        sequence:
          - service: notify.mobile_app_gsm_x
...

If I’m understanding the OP right, the condition is for the tag_id, not the user_id. It’s the check, that this tag really is supported.

automation:
- id: handle_tag_scan
  alias: "Handle Tag Scan"
  mode: single
[...]
  trigger:
    platform: event
    event_type: tag_scanned
  condition:
    # Test that we support this device and tag
    - "{{ trigger.event.data.tag_id in tags }}"
    - "{{ trigger.event.data.device_id in media_players }}"
  action:
    - variables:
        media_player_entity_id: "{{ media_players[trigger.event.data.device_id] }}"
        media_content_id: "{{ tags[trigger.event.data.tag_id].media_content_id }}"
        media_content_type: "{{ tags[trigger.event.data.tag_id].media_content_type }}"
[...]

From here

@Faecon
What isn’t working with the choose? The screenshot you posted before - in that screen go to the upper right and download the trace of the last failed automation and post it here. There should be a reason listed, why it is failing.

On a first glance there seems nothing wrong with the automation, but maybe you post what you have right now, or is it still the same, as in this post above? Just to be on the same page… :wink:

1 Like

the condition is the tag, not the person.

so the trigger is the scanning of a specific tag :
0b3128d7-a0fc-4f56-a7fa-ad6041ea207c

that works.

then it goes further to 3 options (the choose).
I’ve made a test automation

  1. with the user Id
  2. with the attribute id
  3. with the friendly name

They all don’t work so I doubt the

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: "{{ this.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: "{{ 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
mode: single

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