How do I create a unique name for scene within a blueprint?

I am creating a blueprint which is going to generate a scene using the lights that the user specifies.

I will then adjust the brightness of the lights, and eventually restore them by activating the temporary scene that I created in the beginning.

I can easily do this with automations, but I’m trying to convert my automation into a blueprint to make it easier to repeat. The challenge is that I need to programmatically generate a unique name for the temporary scene that is created by the blueprint. If I code the name into the blueprint, it will conflict when the user has multiple instances of their blueprint in use.

Is there a simple way to generate a name from within a blueprint? Can I use the automation ID or something else that would be unique to each instance of the blueprint?

Copy-paste this into the Template Editor to see how it works:

{% set x = 'abcdefghijklmnopqrstuvwxyz' %}
{% set ns = namespace(name='') %}
{% for i in range(15)  %}
  {% set ns.name = ns.name + x | random %}
{% endfor %}
{{ns.name}}

Thats a perfect solution! here is my code in case anyone else comes looking for a similar solution in the future

action:
  - service: scene.create
    data:
      scene_id: >-
        {% set x = 'abcdefghijklmnopqrstuvwxyz' %} {% set ns =
        namespace(name='blueprint_motion_') %} {% for i in range(15)  %}   {%
        set ns.name = ns.name + x | random %} {% endfor %} {{ns.name}}
      entities:
        light.game_light_bulb_1:
          state: 'on'
          brightness: 100
        light.game_light_bulb_2:
          state: 'on'
          brightness: 100
        light.game_light_bulb_3:
          state: 'on'
          brightness: 100
        light.game_light_bulb_4:
          state: 'on'
          brightness: 100
1 Like