Correct syntax to import data from Jinja and use it in a script

I am trying to have some config files in custom_template which I intend to use in several scripts. However I can not figure out the syntax. This is an example with user data and a notify script

people.jinja

{% set people = {
    "alice": {
        "firstname": "Alice",
        "lastname": "Springs",
        "messengerid": "IJKLMNOPQR",
        "email": "[email protected]"
    }
    "bob": {
        "firstname": "Bob",
        "lastname": "Builder",
        "messengerid": "ABCDEFGH",
        "email": "[email protected]"
    },
} %}

My sendmessage.yaml should look something like this

{% from 'people.jinja' import people %}
sendmessage:
  fields:
    username:
      description: "Username of recipient"
      example: "alice"
    message:
      description: "Content of message"
      example: "Hello World"
    sequence:
      - variables:
          id: {{ people[username].messengerid }}
      - action: notify.threema
          data:
            target: "{{ id }}"
            title: "Sent to {{ id }}"
            message: message

This does not work as I can not figure out where to put {% from … import %}. I tried a bunch of places, but either the line is ignored or invalid. Looking at the templating documentation did not resolve this for me.
I appreciate your advice

they can only be imported in a yaml field, you’re trying to import outside yaml.

sendmessage:
  fields:
    username:
      description: "Username of recipient"
      example: "alice"
    message:
      description: "Content of message"
      example: "Hello World"
    sequence:
      - variables:
          people: "{% from 'people.jinja' import people %}{{ people }}"
          id: {{ people[username].messengerid }}
      - action: notify.threema
          data:
            target: "{{ id }}"
            title: "Sent to {{ id }}"
            message: message
1 Like

I spent several hours on this and you solved it in a minute. Guess I still have a lot to learn.
Thanks a million! Will test this and then mark it as resolved

I’ve been doing this for 10 years :slight_smile:

It shows.

I have an additional question. As variables is a dictionary and id can only be set if people has been evaluated before, is this guaranteed to work in the foreseeable future?
I am asking because while lists are sequences, dictionaries are not. This may be different for the parser, though.

No, but it’s an ordered dictionary so it’s extremely unlikely to change. I have ~7 year old automations that rely on this and it hasn’t changed in that 7 years.

I just started Brave which opened the search page prefilled with my question about this topic. Today it responded with an AI generated answer that is obviously based on this thread.
That was quick

The first whole half of that is wrong. You have to import it, you have no choice.

You’re right, and that’s why I think AI is quite limited at this point in time.

I am still surprised this got picked up within hours