Determine user name from a user ID

Hey all, I made my first blueprint and wanted to share.

I wanted to take advantage of the “REPLY” functionality for actionable notifications and know which of the multiple users a notification was sent to replied. After some searching around, I couldn’t find anything in particular. Replies to actionable notifications do, however, include a “user_id” attribute, but there was no easy way to derive the name of the user from the ID.

Until now :slight_smile:

This script blueprint takes in a user ID, determines which user name is associated with it, and then stores the user name in an input_text entity/helper.

blueprint:
  name: Convert User ID to User Name
  description: >-
    A script that determines the user name for a provided user ID.
  domain: script
  source_url: https://github.com/nwithan8/configs/blob/62f5689b6b186e10ba4112b89b0af1ab2416a202/home_assistant/blueprints/scripts/convert_user_id_to_user_name.yaml
  input:
      store_in:
        description: The text input entity to store the result in
        name: Where to store result
        selector:
            target:
              entity:
                domain: input_text
fields:
    user_id:
        description: The ID of the user
        example: abcdef123456
        name: User ID
        required: true
        selector:
            text:
variables:
  store_in: !input 'store_in'
  user_name: >
        {% set user = None %}
    
        {% set matching_users = (states.person |
        selectattr('attributes.user_id','==', user_id) | list) %}
    
        {% if matching_users | length > 0 %}
          {% set user = matching_users | first %}
        {% else %}
          {% set user = "system" %}
        {% endif %}
    
        {{ "System" if user == "system" else state_attr(user.entity_id,
        "friendly_name") }}
sequence:
  - alias: Store calculated user name in text entity
    service: input_text.set_value
    data:
      value: "{{ user_name }}"
    target:
      entity_id: "{{ store_in.entity_id }}"

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

Example usage:

The script:

User ID from a mobile notification response automation passed into the script:

Once updated, the user name can be pulled from the input_text entity and used:

Obviously, if you want to do something similar, make sure to calculate the user name BEFORE using it.

I’m sure there’s other uses for this blueprint. I wanted to keep it simple and the core functionality (figure out which user name this user ID belongs to), rather than making it hyper-specific to actionable notification responses.

Hopefully this helps you all!

4 Likes

Thank you for this! It’s exactly what I was looking for.

1 Like