Create dictionary variable dynamically in blueprint

I have the blueprint for my Lexman controller. It has 4 buttons for switching scenes and some buttons for on/off, brightness, etc.

By pressing scene buttons I save the alias for group of lights. Then when on/off button is pressed I want turn on/off the group of lights depends on alias selected previously.

To avoid a lot of conditions I just want to keep alias/lights mapping. Something like this:

{
"group 1":
  [-light1, -light2],
"group 2":
  [ -light3, -light4]
}

In the code block below I’m trying to use template to prepare the lights_mapping dictionary but it doesn’t work. What is the best practice to create dictionary with dynamic fields?

blueprint:
  name: some name
  domain: automation
  input:
    controller_id:
      name: Switch MQTT id
    group_1_lights:
      name: Group of lights 1
      selector:
        target:
          entity:
            domain: light
    group_1_name:
      name: Name of group 1
    group_2_lights:
      name: Group of lights 2
      selector:
        target:
          entity:
            domain: light
    group_2_name:
      name: Name of group 2

mode: restart

variables:
  name1: !input group_1_name
  name2: !input group_2_name
  lights1: !input group_1_lights
  lights2: !input group_2_lights

  lights_mapping: >
    {% set dict = {} %}
    {% set dict[name1] = lights1 %}
    {{ dict }}

I’m pretty sure Jinja doesn’t let you update a dictionary. So, you have to create it in one statement:

  lights_mapping: "{{ {name1: lights1, name2: lights2} }}"

But I need to use names from input, for example group_1_name

Isn’t that what this does:

variables:
  name1: !input group_1_name
  name2: !input group_2_name
  lights1: !input group_1_lights
  lights2: !input group_2_lights

I can store name. But I can’t use name as the key for dictionary.

Imagine name="some_cool_key".

Is it possible to get such a dict?
lights_mapping: "{{ {"some_cool_key": lights1} }}"

Yes. Dictionary keys can be pretty much anything.

I know. But how can I create such a dict dynamically?
I want to use the value of name1 as the key for my dictionary.

The way I showed you above. Does that not work?

E.g.:

image

image

It doesn’t work in my script. It creates

"{"name1": [light list]}"

Could you share your existing code?

Oh, I’ve found my mistake.

It was:
lights_mapping: {name1: lights1, name2: lights2}

but should
lights_mapping: '{{ {name1: lights1, name2: lights2} }}'

And the next question. Is it possible somehow to create dictionary once when automation starts first time and modify it during automation? The idea that each time automation is triggered we have some data from previous automation run?

Now I use input_text to store json. But it’s not convenient to store a lot of data

If it’s just a couple of lists of entity IDs, you could use a couple of groups instead, which can be updated via the group.set service.

I want to save current state of light entity controller: color preset, temperature color etc. On my controller I can change light groups and then based on current light group state I want to set next color preset from list, for example. But, to select next one, I want to know the id (or name) of the current one which is stored somewhere.

I’ve tried to use var integration but it has some issue at this moment.

And it’s impossible to update dictionaries. You can’t read json from input_text and update one of its fields. You need to create new dict from scratch on the fly to be able to update input_text value.

How about:

image

Result:

image

EDIT:

Looks like this also works:

{% set x = dict(x, **{'b': 10}) %}

You’re not actually updating a dictionary, but creating a new one to replace it which is the same as the old one, but with one of the keys having a new value.

4 Likes

Big thanks!

1 Like

Have another problem. In template editor dict() works as you shown. But when I try to use it in automation the error appears.


Seems like something in your dictionary is not JSON serializable. What’s in the dictionary? (Object of type LoggingUndefined???)

Finally I’ve fixed everything. Thnx for helping.