@panhans
Is the blueprint working for you?
Since a while (updated core) the toggling with static order doesn’t work. The timeout of 10 seconds to use the first scene works, but toggling sadly not.
Can you please try on your side and come back to me?
I cant recreate that issue. I forced the scenes to status unknown and called the script. Everything works as expected. So maybe there was another issue. Do you use the latest version of the blueprint?
//EDIT: forgot to activate static order. my fault
//EDIT2: Now it’s fixed. Until there is no reset for static order and there are scenes with state unknown next in the row with state unknown will be activated.
blueprint:
name: Scene Toggle
description: This script toggles through scenes of a certain area based on its last activated timestamp.
source_url: https://github.com/panhans/homeassistant/blob/4509122e31195e7d836d671c51f7efcac84af7ee/blueprints/script/scene_toggle.yaml
domain: script
input:
area:
name: Name of area
description: Name of the area where you want to toggle scenes
default: ''
selector:
area:
included_scenes:
name: Included Scenes
description: Additional scenes to be included
default: []
selector:
entity:
domain: scene
multiple: true
excluded_scenes:
name: Excluded Scenes
description: Scenes to be excluded
default: []
selector:
entity:
domain: scene
multiple: true
transition:
name: Transition time
description: Transition time when switching between scenes
default: 1
selector:
number:
min: 0
max: 10
step: 0.1
unit_of_measurement: s
use_static_order:
name: "Static order"
description: "This automation is based on timestamps of the activation of the scenes. If this option is enabled a static order will be used instead of the dynamic one."
default: false
selector:
boolean:
reset_after:
name: "Reset After"
description: "If the static order is activated, a timeout can be set after which the order is reset to the first scene. If 0 is set the timeout is disabled."
default: 0
selector:
number:
min: 0
max: 120
step: 1
unit_of_measurement: s
mode: queued
variables:
area: !input 'area'
included_scenes: !input 'included_scenes'
excluded_scenes: !input 'excluded_scenes'
reset_after: !input 'reset_after'
use_static_order: !input use_static_order
last_triggered: "{{ as_timestamp(iif(state_attr(this.entity_id,'last_triggered') == none, now(), state_attr(this.entity_id,'last_triggered'))) }}"
sequence:
- service: scene.turn_on
data_template:
transition: !input transition
entity_id: >
{% set area_scenes = states.scene | selectattr('entity_id', 'in', area_entities(area)) | map(attribute='entity_id') | list %}
{% set all_scenes = (area_scenes | reject('in', excluded_scenes) | list) + included_scenes %}
{% if use_static_order == true %}
{% set is_reset = reset_after > 0 and (as_timestamp(now()) - last_triggered >= reset_after) %}
{% if is_reset == true %}
{{ all_scenes[0] }}
{% else %}
{% set scenes_sorted_by_activation = expand(all_scenes) | sort(attribute='state', reverse = true) | map(attribute='entity_id') | list %}
{% set last_activated_scene = scenes_sorted_by_activation[0] %}
{% set index_of_last_activated_scene = all_scenes.index(last_activated_scene) %}
{% set new_index = index_of_last_activated_scene + 1 %}
{% if new_index == all_scenes | count %}
{{ all_scenes[0] }}
{% else %}
{{ all_scenes[new_index] }}
{% endif %}
{% endif %}
{% else %}
{% set unknown_scenes = expand(all_scenes) | selectattr('state', 'eq', 'unknown') | map(attribute='entity_id') | list %}
{% if unknown_scenes | count > 0 %}
{{ unknown_scenes[0] }}
{% else %}
{% set known_scenes = expand(all_scenes) | sort(attribute='state', reverse = false) | map(attribute='entity_id') | list %}
{{ known_scenes[0] }}
{% endif %}
{% endif %}
It seems it’s an older one.
I saw in the commits that you first change alphabetical → static → alphabetical (with the “unkown” fix/commit).
@panhans you’re welcome.
Thx for the new script.
I tested it and it looked like it worked , or in other words, the previously Manually unknown set scene was taken into account and was triggered.
But now it is stuck at the last scene, and doesn’t start from the beginning of the list. No matter how often i run it.
Reverted to my above posted script and all is working.
Can you please again look into it?
I have tested this way:
Configure the script with 4 scenes, static order with 1 sec transition and 10 sec reset timeout
Set any middle scene to unknown with dev tools
Run the script multiple times to see if the scenes triggers correctly
Reverted to the old one from my post here Simple scene toggle - #53 by pOpY and it’s working again. Without touching the my script (see above).
Just insert old blueprint, save, restart HA, check if it’s working.
So, please try again. For some reason I sorted the scenes by entity_id. That might worked for my naming but not for yours. Next time I’ll make a feature branch again.
Now it’s clear to me why it worked for you and not for me (sorting of the scene names).
The new version is working as expected (unkown scenes are taken into account, the reset after works and the scene toggle loops).
I really love your work here, and this is the first solution for me to toggle between scenes easily. So thank you for that.
The only problem I am facing though, Is that I have made scenes for the living room and the bedroom in z2mqtt. The living room is working perfectly, but I cant get the bedroom to work. I have made the scenes scripts etc in the exact same way.
What can I be doing wrong here? I really dont understand.
If I look at the “traces”, It says
Error: Error rendering data template: ValueError: Undefined is not in list