I created a blueprint that has been very useful for me personally. Hopefully I can help someone else as well by sharing it here. This is the first blueprint I share so any constructive feedback is welcome.
What does it do?
The blueprint creates a script to toggle a specific scene on. And if any entity in the scene is already on, it will turn all entities in the scene off. Because scenes are stateless, this creates a kind of toggle functionality for scenes.
Because the generic home assistant turn off function is used the entities must support this service to work.
Use case
By having a script you can now use a service call to have for example either a physical switch in your home, or a button on your dashboard, toggle on a specific scene in a room. And it will turn all lights off if any light is on in that room (if you have all lights specified in that specific scene).
Blueprint
blueprint:
name: Toggle scene
description: >-
Toggle a scene on and off. If any of the entities is on, switch everything off. Else, activate scene.
domain: script
input:
the_scene:
name: Scene to toggle
description: The scene you want to toggle
selector:
entity:
domain: scene
mode: queued
sequence:
- alias: "Set up variables"
variables:
the_scene: !input the_scene
scene_entities: "{{ state_attr(the_scene, 'entity_id') }}"
- alias: "Toggle the scene"
if:
- condition: template
value_template: |-
{% for entity_ids in scene_entities if is_state(entity_ids, 'on')%}
{% if loop.index == 1 %}
true
{% endif %}
{% else %}
false
{% endfor %}
then:
- service: homeassistant.turn_off
data: {}
target:
entity_id: "{{ scene_entities }}"
else:
- service: scene.turn_on
target:
entity_id: !input the_scene
metadata: {}
Is it possible to add an option to invert the part when: “if any entity is on switch everything off” to “If any entity is on then switch everything on”
Could you eleborate a bit more on what you are looking for? Because the way as I understand it now is that you are looking to activate a scene regardless of the state of the entities. Which would be the same as just turning on the scene.
I have a single button that toggles a scene, the scene is multiple lights and typically 1 light is on but I would prefer when I press the button to toggle the scene on (all lights) it first toggles the single light off then another press toggles the scene on (all lights) on, then another press will switch scene back off.
In that case I think my blueprint should create a script that does exactly what you are looking for, as long as the first light is part of your scene.
It should work like this: if the first light is on, than an entity in the scene is on and the script will turn all entities off, including the first light. Calling the script again would mean all entities are off (because the script did this on the first call) and the scene is activated and your lights are turned on. Calling the script now for a third time (ie pushing your button again) would turn everything off again since there is at least one entity on.
In case your first light is not part of your scene it would still work, but you would have to add it to your scene in the off state.
Just give it a try and let me know if it is operating in an unexpected way.
I don’t know what to your use case would be but you could either create a new scene including all the lights or use the blueprint to create multiple scripts and run all the separate scripts one by one. Would this provide a solution for you?
Question - My programing logic is very rusty. And I for some reason have issues wrapping my head around YAML. I can read some of it — but again its been a long time since programming with a C.
What I understand your loop is doing, is to run the entities array and see if any are on. If so then turn off the entities in the scene.
Do I need to build a variable array to store the values?
Goal to report 3 states - All scene lights on , all off , mixed.
if all on then turn off, if all off then turn on, if mixed then turn on.
Sorry doing this from my phone. It makes not shown the code correctly.
I am doing the logic if one light is on and I use the “scene” button - then turn the other lights on.
If all the lights are on then the assumption is the scene is active. And turn it off. You could add additional code to check for the lights to be at the set level and if they are not. Activate scene. Else turn off.
I discovered blueprints today (been on HA for less than a month) and this one is almost perfect for my need. Nice job
However, in my bedroom, I have a double wireless switch to light up a single ceiling bulb. The left button of the switch (scene 1) turns the light at its full power and the right button (scene 2) turns on the same light, but 50% dimmed.
When the light is off, things are perfectly right : I push the button I need and the scene comes up, but if I want to switch to whatever scene when the other is already on, I first have to turn off the light and then turn it on again on the desired scene.
So, schematically, here is my wish :
From OFF to SCENE 1 : press SCENE 1 button
From OFF to SCENE 2 : press SCENE 2 button
From SCENE 1 to SCENE 2 : press SCENE 2 button only once
From SCENE 2 to SCENE 1 : press SCENE 1 button only once
From SCENE 1 to OFF : press SCENE 1 button
From SCENE 2 to OFF : press SCENE 2 button
For what you wish to perform it is required to know if a specific scene is active. Since scenes don’t have a state in Home Assistant it is not possible to tell if SCENE 1 or SCENE 2 is active. This makes it not possible to implement what you have in mind. Or at least not that I am aware of.
Okay, many thanks for your answer.
Since I posted the message, I got used to my “problem” and it is not that bad.
Once again, nice work, your blueprint is useful for my configuration
Let me see if I got this. It sounds like if you trigger your script to turn a scene on. and a light in the scene is already on, it turns all the lights off. But, I wanted to turn the scene on. So, your script is just “toggling” the group of lights on or off, as you said it would.
I’m coming from the Insteon world of automation where you can turn a scene on or off directly. Sure would love to do this with Home Assistant without a lot of to do. Seems like your Blueprint could be easily modified to do that. Any ideas?
Since a scene can contain many lights, you have to choose a default behavior for what to do when only part of the lights are on. Will you then turn on the other lights as well and activate the scene or will you then turn off all the lights, basically deactivating the scene? I choose to always deactivate the scene, but the script could be modified to do the opposite.
Hi, I wanted to say thanks for your blueprint and let you know my (hopefully clever?) use for it. On my dashboard I wanted a method to let us choose a lighting scene to apply at dusk every day, or on a whim. For instance Halloween, Christmas, or just nightlights. So I created an input select to hold a menu of scenes as well as which one is selected. Then an automation will apply that scene at dusk and a manual input button will do so on a whim, both call the script generated by your blueprint to do the work. This works brilliantly! The real advantage to using your blueprint is being able to turn off a half dozen lights spread around the house with a single click.
I did make one change to the script generated from your blueprint to tie in the input select:
the_scene: scene.{{ states(‘input_select.moodlights’) | lower | replace(’ ', ‘_’) }}