Script: use trigger as start of sensor name

I’m starting Google AI as gimmick in my scripts, to send funny random notifications in my automations.
I can parse the data to the gerative AI and I get a response.
I have some sensors that are starting with the user followed by “_naar_huis” or “_naar werk”.

When I pass {{ person }} through the automation, the name is correctly used.
I want to simplify the automation and use sensor data based on the triggering person.
In this case, it is about the amount of minutes travel time.
I can’t get the persons name parsed into the context, probably something very very simple, but fighting with it for some days already.

In below example I use {{ person }} which returns the triggering person perfectly, but in below statement it is not working.

{{ states(‘sensor.“{{ person }}”_naar_huis’) }}

When I use the following in the developer templates:

{% set person = "marc" %}
{{ person }}
{{ states('sensor.marc_naar_werk') }}
{{ states('sensor.{{ person }}_naar_werk') }}

I get the following response


Automation which is used.

service: google_generative_ai_conversation.generate_content
metadata: {}
  prompt: >-
    Vertel in 1 korte grappige zin dat {{ person }} onderweg is en thuis is over {{ states('sensor."{{ person }}"_naar_huis') }} minuten
response_variable: funnyresponse

You could try this:

{{ states(‘sensor.' + person + '_naar_huis’) }}

Thank you very much! This was key!
:v: :muscle:

1 Like