From 29226c41ab9eafeb48a5432d843170f8eb05c363 Mon Sep 17 00:00:00 2001 From: Nate Harris Date: Fri, 17 Mar 2023 22:38:05 -0600 Subject: [PATCH] New script blueprint to calculate user name from user ID --- .../scripts/convert_user_id_to_user_name.yaml | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 home_assistant/blueprints/scripts/convert_user_id_to_user_name.yaml diff --git a/home_assistant/blueprints/scripts/convert_user_id_to_user_name.yaml b/home_assistant/blueprints/scripts/convert_user_id_to_user_name.yaml new file mode 100644 index 0000000..225cef3 --- /dev/null +++ b/home_assistant/blueprints/scripts/convert_user_id_to_user_name.yaml @@ -0,0 +1,45 @@ +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/master/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 }}"