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

Hi,

I don’t understand how to call a scene created this way.

‘scene.{{ns.name}}’ does not work.

Any hints

Regards

Fabrice

Fabrice, This automation will create a random name each time. So the scene would be ‘scene.asadwwazowehr’ or something.
-David

1 Like

The best-est easiest place I found for a unique ID for stuff is to use one generated for you. When HA runs an automation it generates unique ID’s for the execution “this” and “trigger” actions. It is generated for internal use, but if you want to use it for something else not related to that, to you it’s just a random (& guaranteed unique) ID. I use it as a suggested notification ID for my notify BP, No reason it can’t work for a transient scene ID.

this.context.id

or

trigger.from_state.context.id

or

trigger.to_state.context.id

are 3 that are available on every automation BP execution.

      enter_action:
      - parallel:
        - service: script.personalert_notify
          data:
            person: '{{ trigger.to_state.attributes.friendly_name }}'
            place_to: '{{ trigger.to_state.state }}'
            place_from: '{{ trigger.from_state.state }}'
            notify_id: '{{ trigger.to_state.context.id }}'
            phase: enter
        - service: script.personalert_audio
          data:
            mess: '{{ trigger.to_state.attributes.friendly_name + '' has arrived at
              '' + trigger.to_state.state }}'
            lang: en-GB

Is how I use in, See the notify_id variable.

From a trace:

this:
~~...~~
  context:
    id: 01H14T8SPTHE3CNSN8P5VRS4HR
    parent_id: null
    user_id: null

NOTE:

If you are plugging this into a scene as the ID, you need to pipe it to lower because scene_id is lower_case_chase nomenclature…

{{ trigger.to_state.context.id | lower }}

Just putting this out there in case it will help someone.