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: {}