User ID under this.context.user_id differs from context.user_id

To identify by whom or how a script was invoked, I’ve been using e.g. states.script.shutdown_remote_host.context.user_id. Since then, a this object has been introduced.

I erroneously assumed I could simply replace states.script.shutdown_remote_host with this.

After some debugging, I noticed that context (and thus user_id) appears twice:

this:
  entity_id: script.shutdown_remote_host
  state: 'off'
  attributes:
    last_triggered: '2023-05-30T16:02:08.981343+00:00'
    mode: parallel
    current: 0
    max: 10
    friendly_name: shutdown_remote_host
  last_changed: '2023-11-09T05:24:45.626201+00:00'
  last_updated: '2023-11-09T05:24:45.626201+00:00'
  context:
    id: 01HES8MEXTDZVE91SGD10SRY42
    parent_id: null
    user_id: null
name: CCTV
host: cctv
context:
  id: 01HETAHTP8ZD5XTNTBYYF71KRZ
  parent_id: null
  user_id: 75cfafa25f36464abc9116845e53f2ca

What I don’t get and can’t seem to find any info or posts on, is why this.context.user_id would be null when context.user_id is defined.

Is this a bug or something I’m misunderstanding about contexts?

How I’m using this:

            {% set user_id = this.context.user_id %}
            {% set triggered_by = (states.person | selectattr('attributes.user_id','==', user_id)) | list %}
            {% set first_name = "System" if not triggered_by else state_attr((triggered_by | first).entity_id, "friendly_name").split()[0] %}
            {{ first_name }} shut down {{ name }}.

Problematic:

{% set user_id = this.context.user_id %}

Existing implementation that works:

{% set user_id = states.script.shutdown_remote_host.context.user_id %}

You con’t have to use this, you can just refer to the context variable directly. So:

{% set user_id = context.user_id %}

In the version you used, you could still make it dynamic by using:

{% set user_id = states[this.entity_id].context.user_id %}

I don’t know for sure why it differs from the this one. My guess is that the this variable gives the data when the script was started, so basically the previous data (which is similar to the this object in an automation). This also seems to match with the last_triggered value in the this object, as that was in May this year, and I assume you didn’t wait 6 months with posting this question.

1 Like

Ah, excellent. I wasn’t aware I could shorten that first one.

Luckily I only have 3 places where I use this, so it hasn’t bothered me, but good to be reminded of that option.

Background: Keen eye there… I did indeed last run this in May (and didn’t wait that long). I used to use this to shut down hosts on UPSs when the power got cut. Now I have a proper backup power system, so it’s not really used anymore (until when I had to manually run the script from the UI to shut down a host).

So, if I follow you, this.context will have historical data, which would make sense, because the last time it was run it would’ve been from an automation. Just using context is thus more current, in a sense.