Querying a script attribute in the script itself

If I need to request the value of a script attribute in the script itself, e.g. its friendly_name, can I simply point to the current script in such a request {{state_attr(‘script.vanna’, ‘friendly_name’)}} or can I request it differently?

I would like to get a universal friendly_name query for all scripts to output error messages, which is the same in many of my scripts and only the script name changes

‘this’ gives you info about the script.

Make a script and add an action of:

action: notify.persistent_notification
data:
  message: >-
    this-{{ this }}<br>
    this.entity_id-{{ this.entity_id }}

Look at your notifications and see the output. i showed ‘this’ and ‘this.entity_id’ as examples. You can see the rest in there.

1 Like

it doesn’t work on my example, there must be an error somewhere ?
title: “Name: {{state_attr(‘script.this’, ‘friendly_name’)}}”

So you just made stuff up and did not follow what I showed you?

Let me add another example:

action: notify.persistent_notification
data:
  message: >-
    this-{{ this }}<br>
    this.entity_id-{{ this.entity_id }}<br>
    this.attributes.friendly_name-{{ this.attributes.friendly_name }}

Whatcha doing with ‘title:’? How are you expecting to output that info? Give more info or there is no way to help trouble shoot anything.

1 Like
action: notify.telegram
metadata: {}
data:
  message: повторился 60 раз и не выполнился
  title: "Скрипт: {{ this.attributes.friendly_name }}"

tried to use this code, but got an error

Error rendering data template: UndefinedError: ‘this’ is undefined

It seems you are attempting to do this from a spot where ‘this’ is not valid.

Where is this code you are using? What I have shown is in the script.

Show your whole yaml for what you are testing. Not from the blue circle. But from the red circle please.

sequence:
  - action: light.turn_on
    metadata: {}
    data: {}
    target:
      entity_id: light.led_vanna_stena_pitanie
  - delay:
      hours: 0
      minutes: 0
      seconds: 1
  - repeat:
      sequence:
        - delay:
            hours: 0
            minutes: 0
            seconds: 1
        - parallel:
            - continue_on_error: true
              action: light.turn_on
              metadata: {}
              data: {}
              target:
                entity_id: light.led_stena_vanna2
            - continue_on_error: true
              action: light.turn_on
              metadata: {}
              data: {}
              target:
                entity_id: light.rgbw_3
            - if:
                - condition: template
                  value_template: >-
                    {% if  repeat.index==60 and
                    (states('light.led_stena_vanna2') != 'on' or
                    states('light.rgbw_3') != 'on')%}

                    True

                    {%endif%}
              then:
                - action: notify.telegram
                  metadata: {}
                  data:
                    message: повторился 60 раз и не выполнился
                    title: >-
                      this.attributes.friendly_name-{{
                      this.attributes.friendly_name }}
      until:
        - condition: template
          value_template: >-
            {% if  repeat.index>=60 or (states('light.led_stena_vanna2') == 'on'
            and states('light.rgbw_3') == 'on')%}

            True

            {%endif%}
alias: Ванна стена при движении после подачи питания
description: ""
fields: {}

I don’t use telegram. So, I can’t test what you have. But can you test the persistent notification I provided to see if it is working for you?

If it is then you need to figure out the difference between the two. If not working then we can work on that.

Oh. I think I know what is happening. You may be losing ‘this’ due to the loop you have in there. You need to do a search on the scope of variables. Might need to setup a namespace outside the loop and set a variable inside there to hold ‘this’.

I might be able to test this and reply late this coming week if someone does not help in the mean time.

I could not replicate your example, I put the alert at the very top of the script on top of all loops same error
I changed the notification method using just a push to my smartphone, the error is still there

what kind of notification method is working for you ?

The error you are getting is due to it still being in the loop I believe.

Do a new simple script with what I gave and test it.

That definitely should not be what is happening. In neither HASS nor Jinja should there be any issues with accessing variables inside a loop that were defined outside of it.

What you are talking about (namespaces) is the reverse, how to make results from inside of a loop accessible on the outside, in Jinja. (The closest analog for HASS itself would be using helpers).

It is not uncommon for me to get things backwards :rofl:

Not sure what is causing the issue here then.

Everything worked thanks, the error was due to the fact that I ran a specific action, not the whole script!

1 Like

Well, that makes sense.

Glad it is working.

Please give ALL the info when debugging issues :slight_smile:

1 Like