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}}
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.