⏭ Simple Scene Toggle

ATM its not simply possible to implement that behaviour because the list is sorted dynamically based on the last activation time stamp.
It needs a 2nd option to iterate through the list as it’s given. Than a timeout would be possible. I am going to implement that. Maybe this evening or the next days.

Thanks for the fast reply and also for looking into it.
PS.: Dont hurry, take your time.

1 Like

Here is a testing branch for you. I rewrote the logic a little bit. Just activate alphabetical order and set your timeout. Waiting for your feedback. If it’s positive I will merge the changes in the main branch.

1 Like

Thanks a lot. Will definitely try it!
Sadly, i am currently waiting for my new x86 hardware.
Because with my arm (odroid n2+) i am facing an kernel issue with the sonoff stick: https://github.com/Koenkk/zigbee2mqtt/issues/12566#issuecomment-1465295006
ftdi_sio ttyUSB0: usb_serial_generic_read_bulk_callback - urb stopped: -32 · Issue #2406 · raspberrypi/linux · GitHub

Will come back here when new HW is up and running.

1 Like

@panhans Now running the new hardware and found time to test the new branch. It’s working but i dont need the scenes in alphabetical order feature. I’ll need the sorting of the scenes like configured in the script.

Can you please make the “alphabetical” optional for the timeout feature?

thx

Hey, give it a 2nd try, pls.

1 Like

@panhans Tested with two scenes and working as it should. Thanks a lot!!!

1 Like

@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?

Thx

1 Like

I think i found it! Since scenes did had the state unknown, see watchman report:


-== Watchman Report ==- 

-== Congratulations, all 82 services from your config are available!

-== Missing 19 entity(ies) from 424 found in your config:
+--------------------------------+---------+--------------------------------------------------------------+
| Entity ID                      | State   | Location                                                     |
+--------------------------------+---------+--------------------------------------------------------------+
| scene.schlafzimmer_nachtlicht  | unknown | scripts.yaml:616                                             |
| ('Schlafzimmer Nachtlicht')    |         |                                                              |
| scene.essbereich_gedimmt       | unknown | scripts.yaml:1069                                            |
| ('Essbereich Gedimmt')         |         |                                                              |
| scene.essbereich_nachtlicht    | unknown | scripts.yaml:1070                                            |
| ('Essbereich Nachtlicht')      |         |                                                              |
| scene.schlafzimmer_lesen       | unknown | automations.yaml:3016                                        |
| ('Schlafzimmer Lesen')         |         |                                                              |
| scene.bad_nachtlicht ('Bad     | unknown | .storage/lovelace.lovelace_bad:32                            |
| Nachtlicht')                   |         |                                                              |
| scene.bad_lesen ('Bad Lesen')  | unknown | .storage/lovelace.lovelace_bad:37                            |
| scene.bad_entspannen ('Bad     | unknown | .storage/lovelace.lovelace_bad:42                            |
| Entspannen')                   |         |                                                              |
| scene.arbeitszimmer_hell       | unknown | .storage/lovelace.lovelace_arbeitszimmer:26                  |
| ('Arbeitszimmer Hell')         |         |                                                              |
| scene.arbeitszimmer_nachtlicht | unknown | .storage/lovelace.lovelace_arbeitszimmer:36                  |
| ('Arbeitszimmer Nachtlicht')   |         |                                                              |
| scene.arbeitszimmer_lesen      | unknown | .storage/lovelace.lovelace_arbeitszimmer:41                  |
| ('Arbeitszimmer Lesen')        |         |                                                              |
| scene.arbeitszimmer_entspannen | unknown | .storage/lovelace.lovelace_arbeitszimmer:46                  |
| ('Arbeitszimmer Entspannen')   |         |                                                              |
| scene.arbeitszimmer_konzentrat | unknown | .storage/lovelace.lovelace_arbeitszimmer:51                  |
| ion ('Arbeitszimmer            |         |                                                              |
| Konzentration')                |         |                                                              |
| scene.schlafzimmer_entspannen  | unknown | .storage/lovelace.lovelace_schlafzimmer:42                   |
| ('Schlafzimmer Entspannen')    |         |                                                              |
| scene.schlafzimmer_konzentrati | unknown | .storage/lovelace.lovelace_schlafzimmer:47                   |
| on ('Schlafzimmer              |         |                                                              |
| Konzentration')                |         |                                                              |
| scene.wohnzimmer_gedimmt       | unknown | .storage/lovelace.lovelace_wohnzimmer:46                     |
| ('Wohnzimmer Gedimmt')         |         |                                                              |
| scene.wohnzimmer_entspannen    | unknown | .storage/lovelace.lovelace_wohnzimmer:65                     |
| ('Wohnzimmer Entspannen')      |         |                                                              |
| scene.wohnzimmer_konzentration | unknown | .storage/lovelace.lovelace_wohnzimmer:70                     |
| ('Wohnzimmer Konzentration')   |         |                                                              |
| scene.wohnzimmer_aktivieren    | unknown | .storage/lovelace.lovelace_wohnzimmer:74                     |
| ('Wohnzimmer Aktivieren')      |         |                                                              |
| scene.vorraum_gedimmt          | unknown | .storage/lovelace.lovelace_vorraum:27                        |
| ('Vorraum Gedimmt')            |         |                                                              |
+--------------------------------+---------+--------------------------------------------------------------+

-== Report created on 10 Jun 2023 10:51:25
-== Parsed 42 files in 0.56s., ignored 23 files 
-== Generated in: 0.01s. Validated in: 0.00s.

Because of that, the blueprint didn’t know when it was the last time it triggered and that issue.
Manually activating the scenes fixed the issue.

Is the time stamp of scenes stored permanently?
Do you have an idea how to handle scenes with unknown state?

Thx

1 Like

Hi, thanks for reporting. I will have a look into this issue the next week. Had a problem with it some time ago and I thought I had it solved.

1 Like

Thx.
Take your time. My workaround (activating all scenes manually) solved it for now.

Just let me know if I can help (test) any further :slightly_smiling_face:

1 Like

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 :wink:

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

I have this version running:

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

Is this wanted?

I can test the newest version in the evening.

thx

Thanks for the hint. My local version hasn’t changed the naming. I fixed it. Would be nice if you can test it.

@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? :slightly_smiling_face:

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

I am here to test. Just let me know.

Thx
pOpY

1 Like

Thanks for your help. You can test again. Think it will work as expected now.

@panhans Thx for the new version.
Unknown scenes will be taken into account now and also the “loop” starts from the beginning.

Sadly the set 10 secs “Reset after” doesn’t work now.

Can you please look into it again :sweat_smile:
Thx

PS: i am here to test

For me the timeout works as well. I comitted my local code to github again but it was the latest version already.

For me not. Tested again.

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.

Here is my yaml:

alias: Wohnzimmer Scene Toggle
use_blueprint:
  path: panhans/scene_toggle.yaml
  input:
    included_scenes:
      - scene.wohnzimmer_hell
      - scene.wohnzimmer_lesen
      - scene.wohnzimmer_nachtlicht
      - scene.wohnzimmer_fernsehlicht
    use_static_order: true
    reset_after: 10
mode: single

and checked that i am using the newest script version from github with bexond compare, they are the same:

Any hints?
How can i help you to debug?

PS.: Also tried to create a new automation with the blueprint and the same date (input per hand, not yaml) and the same issue

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. :smiley: