Skip to content

Instantly share code, notes, and snippets.

@mikehosmar
Forked from HarvsG/toggle-scene.yaml
Last active June 20, 2023 05:02
Show Gist options
  • Save mikehosmar/a06b4edb52454a6107b71471cb0d4576 to your computer and use it in GitHub Desktop.
Save mikehosmar/a06b4edb52454a6107b71471cb0d4576 to your computer and use it in GitHub Desktop.
HomeAssistant Blueprint to toggle scenes
blueprint:
name: Toggle Scenes
description:
Trigger this automation from another automation using This will allow
you to use a single trigger to toggle a scene on or off. A timeout variable is
given, after which the trigger will activate the scene irrespective of the state
of the last toggle.
domain: automation
input:
scene_input:
name: Scene to be toggled
description: This is the scene that will be turned on and off
selector:
entity:
domain:
- scene
multiple: false
no_scene_wait:
name: Timeout
description:
The time after which the original scene will be activated even
if the next toggle state would have been scene off
default: 240
selector:
number:
min: 0.0
max: 3600.0
unit_of_measurement: minutes
step: 1.0
mode: slider
scene_transition:
name: Transition Time
description: The transition time for changing scenes
default: 2.5
selector:
number:
mode: box
min: 0
max: 600
step: 0.1
unit_of_measurement: seconds
trigger_variables:
scene_entity: !input scene_input
scene_transition: !input scene_transition
scene_store_name: "{{scene_entity.split('.')[1] + '_store_previous_state'}}"
scene_store_entity: "{{'scene.' + scene_store_name}}"
time_out_seconds: !input no_scene_wait
time_out: "{{time_out_seconds*60}}"
trigger:
- platform: event
event_type: scene_toggle
event_data:
entity_id: "{{scene_entity}}"
action:
if:
- condition: template
value_template: "
{{(states(scene_store_entity) == 'unknown') or (0 < as_timestamp(states(scene_store_entity),0)
- as_timestamp(states(scene_entity),0)) or (as_timestamp(now()) - as_timestamp(states(scene_entity),0)
> time_out)}}"
alias:
If film_before activated more recently than film mode or if film mode has
not been activated for more than 4 hours
then:
- service: scene.create
data:
scene_id: "{{scene_store_name}}"
snapshot_entities: "{{ state_attr(scene_entity, 'entity_id') }}"
- if:
- condition: template
value_template: "{{(states(scene_store_entity) == 'unknown')}}"
alias: If scene before has never been activated before
then:
- service: scene.turn_on
data:
transition: >
{% if trigger.event is defined %}
{{trigger.event.data.transition | default(scene_transition)}}
{% else %}
{{scene_transition}}
{% endif %}
target:
entity_id: "{{scene_store_entity}}"
- service: scene.turn_on
target:
entity_id: "{{scene_entity}}"
data:
transition: >
{% if trigger.event is defined %}
{{trigger.event.data.transition | default(scene_transition)}}
{% else %}
{{scene_transition}}
{% endif %}
else:
- service: scene.turn_on
data:
transition: >
{% if trigger.event is defined %}
{{trigger.event.data.transition | default(scene_transition)}}
{% else %}
{{scene_transition}}
{% endif %}
target:
entity_id: "{{scene_store_entity}}"
@mikehosmar
Copy link
Author

Added transition time. You can set a default or pass it along with the event trigger.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment