Brainstorming a way to switch scene's on a button press

Howdy,

I have the following scenario, i have a regular light switch connected to a shelly, so i get a on and a off state. To make sure i dont trigger from anything an undefined state i switch only on the state change from on to off and from off to on.

On a trigger i want to:
On first press,:

  • if any light is on, turn them om.
  • if no lights are on, turn them on 100%

subsequent presses should:

  • if you turned the light to 100%, now go 50%
  • if you set the light to 50% now go off
  • if you set the light off, now go 100%

Before i used this this nifty peace of code in node red, which is very easy to expand with more conditions.

export [{"id":"3d958283059b7308","type":"api-call-service","z":"9d267bfa767e8a46","name":"Turn on with given parameters","server":"90c6e8da.709198","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":[],"entityId":["light.0_woonkamer_bank_lamp_leeshoek","light.0_woonkamer_bank_spot_links","light.0_woonkamer_bank_spot_rechts"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":830,"y":240,"wires":[[]]},{"id":"a9bd982346aba1d9","type":"api-call-service","z":"9d267bfa767e8a46","name":"Turnoff","server":"90c6e8da.709198","version":5,"debugenabled":false,"domain":"light","service":"turn_off","areaId":[],"deviceId":[],"entityId":["light.0_woonkamer_bank_lamp_leeshoek","light.0_woonkamer_bank_spot_links","light.0_woonkamer_bank_spot_rechts"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":880,"y":140,"wires":[[]]},{"id":"60b7c4dfa092fa24","type":"api-current-state","z":"9d267bfa767e8a46","name":"Bank=On","server":"90c6e8da.709198","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.0_woonkamer_bank_spot_links","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":0,"forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":520,"y":60,"wires":[["13db3a385d4109ea"],["c60a3e73392efc58"]]},{"id":"3929e74b611f7362","type":"api-current-state","z":"9d267bfa767e8a46","name":"BankLL=On","server":"90c6e8da.709198","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.0_woonkamer_bank_lamp_leeshoek","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":0,"forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":530,"y":140,"wires":[["13db3a385d4109ea"],["ac8a21acb67e1b01"]]},{"id":"ac8a21acb67e1b01","type":"function","z":"9d267bfa767e8a46","name":"Scene switcher","func":"//var count = context.get(\"counter\");\n\n// initialise the counter to 0 if it doesn't exist already\nvar counter = context.get('counter');\n\nvar NOC = node.outputCount\n//msg.noc = NOC;\n\nif (msg.payload == \"Reset\")\n{\n context.set('counter',0);\n return\n}\nelse\n{\n counter += 1;\n if (counter == node.outputCount)\n {context.set('counter',0); msg.payload = \"reset counter\";}\n else\n {context.set('counter',counter); msg.rep = 1; msg.payload = \"increase counter\"}\n}\n\nmsg.count = counter;\nif (counter == 1)\n{\n return [msg];\n} \nif (counter == 2)\n{\n return [null,msg];\n}\nif (counter == 3)\n{\n return [null,null,msg];\n}\nif (counter == 4)\n{\n return [null,null,null,msg];\n}","outputs":3,"timeout":"","noerr":0,"initialize":"// Code added here will be run once\n// whenever the node is started.\nif (context.get(\"counter\") === undefined) {\n context.set(\"counter\", 0)\n}\n","finalize":"","libs":[],"x":260,"y":200,"wires":[["5c72f13b07c638fe","60b7c4dfa092fa24"],["5c72f13b07c638fe","e8fb5f7642525899"],["cc1e4c3f2aea8683"]]},{"id":"5c72f13b07c638fe","type":"trigger","z":"9d267bfa767e8a46","name":"","op1":"","op2":"Reset","op1type":"nul","op2type":"str","duration":"3","extend":true,"overrideDelay":false,"units":"s","reset":"on","bytopic":"all","topic":"topic","outputs":1,"x":260,"y":140,"wires":[["ac8a21acb67e1b01"]]},{"id":"e8fb5f7642525899","type":"function","z":"9d267bfa767e8a46","name":"100%","func":"msg.payload = \n{\n\"data\": {\n \"transition\":\"1\",\n \"brightness\":\"255\"\n }\n};\nreturn [msg];","outputs":1,"timeout":"","noerr":0,"initialize":"// Code added here will be run once\n// whenever the node is started.\nif (context.get(\"counter\") === undefined) {\n context.set(\"counter\", 0)\n}\n","finalize":"","libs":[],"x":490,"y":220,"wires":[["6425268fb368e5d1"]]},{"id":"cc1e4c3f2aea8683","type":"function","z":"9d267bfa767e8a46","name":"50%","func":"msg.payload = \n{\n\"data\": {\n \"transition\":\"1\",\n \"brightness\":\"125\"\n }\n};\nreturn [msg];","outputs":1,"noerr":0,"initialize":"// Code added here will be run once\n// whenever the node is started.\nif (context.get(\"counter\") === undefined) {\n context.set(\"counter\", 0)\n}\n","finalize":"","libs":[],"x":490,"y":260,"wires":[["6425268fb368e5d1"]]},{"id":"32697471a0b2d10d","type":"link in","z":"9d267bfa767e8a46","name":"In_S_T_Shelly_Bank_Input","links":["ee464e442f6f8044"],"x":115,"y":200,"wires":[["ac8a21acb67e1b01"]]},{"id":"c60a3e73392efc58","type":"api-current-state","z":"9d267bfa767e8a46","name":"Bank=On","server":"90c6e8da.709198","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.0_woonkamer_bank_spot_rechts","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":0,"forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":520,"y":100,"wires":[["13db3a385d4109ea"],["3929e74b611f7362"]]},{"id":"13db3a385d4109ea","type":"junction","z":"9d267bfa767e8a46","x":680,"y":100,"wires":[["a9bd982346aba1d9"]]},{"id":"6425268fb368e5d1","type":"junction","z":"9d267bfa767e8a46","x":640,"y":240,"wires":[["3d958283059b7308"]]},{"id":"90c6e8da.709198","type":"server","name":"Home Assistant","version":5,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30,"areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true}]

But since i’m migrating to home asistant automations i had to think of something else:

This brainstorming and trying to explain it to strangers helped me so i though why not share, and maby even get some feedback to improve:

so here is what i build in home assistant:

alias: LightSwitchingBetweenScenes
description: "First test of automation to switch between scene's"
trigger:
  - platform: state
    entity_id:
      - binary_sensor.0_wnk_btn_naastdeur_tafel
    from: "on"
    to: "off"
    alias: Button pressed up
  - platform: state
    entity_id:
      - binary_sensor.0_wnk_btn_naastdeur_tafel
    from: "off"
    to: "on"
    alias: Button pressed down
condition: null
action:
  - if:
      - condition: template
        value_template: "{{ LastRunWasXSecondsAgo > 5 }}"
        alias: If last run was longer then 5 seconds ago
    then:
      - alias: Execute first press procedure, turn on or off based on state
        if:
          - condition: state
            entity_id: light.0_wer_lmp_group
            state: "on"
        then:
          - service: scene.turn_on
            metadata: {}
            target:
              entity_id: scene.0_wer_lmp_group_0_all_off
        else:
          - service: scene.turn_on
            metadata: {}
            target:
              entity_id: scene.0_wer_lmp_group_1_all_on
    else:
      - choose:
          - conditions:
              - alias: if you turned the light to 100%, now go 50%
                condition: and
                conditions:
                  - alias: All on before All Off
                    condition: template
                    value_template: >-
                      {{ as_timestamp(states('scene.0_wer_lmp_group_1_all_on'))
                      > as_timestamp(states('scene.0_wer_lmp_group_0_all_Off'))
                      }}  
                  - alias: All on before All 50
                    condition: template
                    value_template: >-
                      {{ as_timestamp(states('scene.0_wer_lmp_group_1_all_on'))
                      > as_timestamp(states('scene.0_wer_lmp_group_2_all_50'))
                      }}  
            sequence:
              - service: scene.turn_on
                metadata: {}
                target:
                  entity_id: scene.0_wer_lmp_group_2_all_50
          - conditions:
              - alias: if you set the light to 50% now go off
                condition: and
                conditions:
                  - alias: All 50 before All Off
                    condition: template
                    value_template: >-
                      {{ as_timestamp(states('scene.0_wer_lmp_group_2_all_50'))
                      > as_timestamp(states('scene.0_wer_lmp_group_0_all_Off'))
                      }}  
                  - alias: All 50 before All on
                    condition: template
                    value_template: >-
                      {{ as_timestamp(states('scene.0_wer_lmp_group_2_all_50'))
                      > as_timestamp(states('scene.0_wer_lmp_group_1_all_On'))
                      }}                    
            sequence:
              - service: scene.turn_on
                metadata: {}
                target:
                  entity_id: scene.0_wer_lmp_group_0_all_off
          - conditions:
              - alias: if you set the light off, now go 100%
                condition: and
                conditions:
                  - alias: All off before all on
                    condition: template
                    value_template: >-
                      {{ as_timestamp(states('scene.0_wer_lmp_group_0_all_off'))
                      > as_timestamp(states('scene.0_wer_lmp_group_1_all_On'))
                      }}
                  - alias: All off before All 50
                    condition: template
                    value_template: >-
                      {{ as_timestamp(states('scene.0_wer_lmp_group_0_all_off'))
                      > as_timestamp(states('scene.0_wer_lmp_group_2_all_50'))
                      }}
            sequence:
              - service: scene.turn_on
                metadata: {}
                target:
                  entity_id: scene.0_wer_lmp_group_1_all_on
variables:
  alias: Last Run Was X Seconds Ago
  LastRunWasXSecondsAgo: >
    {{ as_timestamp(now()) -
    as_timestamp(state_attr(this.entity_id,'last_triggered')) }}  
mode: queued

I’m noticing the yaml code doesnt copy and past nice into the < details > hidy hole i used for the node red code, if there is a better way to paste and show it please let me know, looking forward to sharing more code here :slight_smile:

I will try to make this into a script which will take in the scenes as a variable, but thats for later expansion. Also expanding for more then 3 scenes would give code bloat, so any tips to make it more compact are always welcome.

Also i am considering different behavior at night and during the day, but i guess once i make a script, i can just change up the order and call the correct script for the current TOD/brightness outside.

one thing that took me some time, I had to use the LastRunWasXSecondsAgo variable, because as_timestamp(state_attr(this.entity_id,'last_triggered')) would already be updated to the current time in the action. and funny enough you don’t see the

variables:
  alias: Last Run Was X Seconds Ago
  LastRunWasXSecondsAgo: >
    {{ as_timestamp(now()) -
    as_timestamp(state_attr(this.entity_id,'last_triggered')) }}  

part in the visual editor

if you’re looking for ideas to simplify, take a look at this:

alias: LightSwitchingBetweenScenes
description: First test of automation to switch between scene's
trigger:
  - platform: state
    entity_id:
      - binary_sensor.0_wnk_btn_naastdeur_tafel
    from: "off"
    to: "on"
    alias: Button pressed down
condition: []
action:
  - service: light.toggle
    target:
      entity_id: light.0_wer_lmp_group
  - repeat:
      sequence:
        - wait_for_trigger:
            - platform: state
              entity_id:
                - binary_sensor.0_wnk_btn_naastdeur_tafel
              from: "off"
              to: "on"
          timeout:
            seconds: 5
          continue_on_timeout: false
        - service: light.turn_on
          target:
            entity_id:
              - light.0_wer_lmp_group
          data:
            brightness: |
              {% if states('light.0_wer_lmp_group') == 'off' %}
                 255
              {% elif state_attr('light.0_wer_lmp_group','brightness') > 200 %}
                 128
              {% else %}
                 0
              {% endif %}
      while:
        - condition: template
          value_template: "{{ true }}"

one thing i don’t understand about yours,you have a trigger on button press and button release. doesn’t that trigger your code twice as often as desired? doesn’t it change the light on press then on release?

1 Like

Wow, that repeat function with wait_for_trigger is great! That will simplify my life a lot and save a lot of coding. Thanks :+1: :pray:

I figured using scenes would be nice, but when only setting all lights to the same brightness it might be overkill.

However, in other places in my house, I also use scenes that exclude certain lights when less brightness is required. So there, scenes might be advantageous/faster than calling all lights individually.

So I definitely want to use scenes there. The question becomes, should I mix scenes with group commands or only use scenes? I’m not sure yet; I will have to experiment.

I’m probably going to try something with one repeat loop and multiple wait_for_triggers. I will post back when I have made something I am happy with!

Actually, in the Netherlands, regular household buttons are toggle switches, not “momentary/push” switches.


(We use the right one in NL, but I added the left one for functional clarity.)

So, one press goes from the off state to the on state, and the next press does it the other way around. I want to trigger on any press, whether it’s from off to on or from on to off.

ah, thank you for the explanation. the switches than to have of that sort present themselves as switch entities, not binary_sensors. but regardless, that makes sense now.

yes I am taking advantage of your lights being homogeneous.

one other thing is that my first call is to just toggle the lights. this does the right thing that you asked for if one light in the group is on.

If u want to use scenes here’s something that may help you… I am not in a position to code right now, but I am grabbing this snippet and since you sound quite capable, I hope it is enough…

{% set entities = ['sensor.battery_158d00020f1b8c', 'binary_sensor.switch_158d00020f1b8c'] %}
{% set last = entities | expand | sort(attribute='last_updated', reverse=True) | first %}

in this example, I have a list of entities.
. Doesn’t need to be homogeneous… In your case you would put all of your scenes. the sort will then tell you which was most recently updated.

I will check if i can configure my switches correctly thanks for the feedback, this is the default that shelly passed on to me.

I simplified the trigger a bit for testing purposes, but I’m at this now:

alias: TestInoPlafond
description: ""
trigger:
  - platform: state
    entity_id:
      - input_boolean.ino_helper_bool
    from: "on"
    to: "off"
condition: null
action:
  - alias: Default Action
    if:
      - condition: template
        value_template: "{{ state_attr('light.0_wer_lmp_group', 'brightness') >1 }}"
    then:
      - service: scene.turn_on
        metadata: {}
        target:
          entity_id: scene.0_wer_lmp_group_0_all_off
    else:
      - repeat:
          for_each:
            - scene: scene.0_wer_lmp_group_1_all_on
            - scene: scene.0_wer_lmp_group_2_all_50
          sequence:
            - service: scene.turn_on
              metadata: {}
              target:
                entity_id: "{{ repeat.item.scene }}"
            - if:
                - condition: template
                  value_template: "{{repeat.last == false}}"
              then:
                - wait_for_trigger:
                    - platform: state
                      entity_id:
                        - input_boolean.ino_helper_bool
                      from: "on"
                      to: "off"
                  timeout:
                    seconds: 5
                  continue_on_timeout: false
            - service: notify.persistent_notification
              metadata: {}
              data:
                message: "{{ repeat | tojson }}"
                title: "{{ repeat.last == true }}"
mode: single

this allows easy adding of multiple scenes

the Repeat and wait_for_trigger amazing, the only thing I’m not happy with is the that I had to put the wait_for_trigger behind an if statement. I tried getting the

"{{repeat.last == false}}"

as a condition for the wait_for_trigger, but it is really looking for a trigger not a condition so i didn’t get it to take it.

i got the sort function you proposed working as wel, but it has the disadvantage that it will just start the last item used. so you don’t know if it will go scene 1-2-3-4 or scene 1-4-2-3, and i want consistency in the order!

Maybe I’ll try to create a script which accepts any number of scenes, to make it easier to change or expand later on. But this code is clean enough for me to deploy in my house now :slight_smile:

have you tested that code and it works well? there are several parts that confuse me.

i must admit, i’m not sure what the reason for the if repeat.last is for.

technically you can put it in the wait_for_trigger, however the repeat.last doesn’t change during the wait. it either is or it isn’t. so the wait_for_trigger will always wait the full 5 seconds. what were you trying to accomplish with putting it in the wait.

for me, this version is a little confusing. but that’s just me…

wrt the “last changed”… that i sent, you can still control the order. here’s an example. note that i’m mostly pseudo coding it. i’m sure there are syntax errors or such but since you clearly know how to code, i’m focusing on giving you the concept.

also note how i did the state to and from. you don’t need to have 2 triggers, you can combine your two triggers into one.

alias: LightSwitchingBetweenScenes
description: First test of automation to switch between scene's
trigger:
  - platform: state
    entity_id:
      - binary_sensor.0_wnk_btn_naastdeur_tafel
    from: 
      - "off"
      - "on"
    to: 
      - "on"
      - "off"
condition: []
action:
  - service: light.toggle
    target:
      entity_id: light.0_wer_lmp_group
  - repeat:
      sequence:
        - wait_for_trigger:
            - platform: state
              entity_id:
                - binary_sensor.0_wnk_btn_naastdeur_tafel
              from: 
                - "off"
                - "on"
              to: 
                - "on"
                - "off"
          timeout:
            seconds: 5
          continue_on_timeout: false
        - service: scene.turn_on
          target:
            entity_id: >
              {% set entities = ['scene.0_wer_lmp_group_0_all_off', 'scene.0_wer_lmp_group_2_all_50', 'scene.0_wer_lmp_group_1_all_on'] %}
              {% set last = entities | expand | sort(attribute='last_updated', reverse=True) | first %}
              {% if last == 'scene.0_wer_lmp_group_0_all_off'%}
                 scene.0_wer_lmp_group_1_all_on
              {% elif last == 'scene.0_wer_lmp_group_1_all_on' %}
                 scene.0_wer_lmp_group_2_all_50
              {% else %}
                 scene.0_wer_lmp_group_0_all_off
              {% endif %}
      while:
        - condition: template
          value_template: "{{ true }}"

for me, this approach seems clearer and also easy to add to and adjust for other cases where you want to have multiple scenes triggered in sequence.

It is definitely less code and more concise, and if you’re fully comfortable with Jinja 2, it’s easier to read!

At first, I thought the toggle wouldn’t work because if you turned the lights off after they were at 50%, and then later turned them back on, it would toggle to the 50% state. However, this fixes that:

  - service: light.toggle
    metadata: {}
    data:
      brightness_pct: 100
      transition: 1
    target:
      device_id: ba6535b7f0b4fbcbb58ee807b7c8cf32

and it is cleaner to do a toggle and then switch between the states for sure, however this removes the option to have the first press choose between off and a scene in which not all lights do the same. so i did not opt for this solution. (though fully compliant with the first requirements i put down).

this part:

              from: 
                - "off"
                - "on"
              to: 
                - "on"
                - "off"

also GREAT! didnt know you could add that, that saves me alot! and will get used in other parts of my code as wel!

              {% set entities = ['scene.0_wer_lmp_group_0_all_off', 'scene.0_wer_lmp_group_2_all_50', 'scene.0_wer_lmp_group_1_all_on'] %}
              {% set last = entities | expand | sort(attribute='last_updated', reverse=True) | first %}
              {% if last == 'scene.0_wer_lmp_group_0_all_off'%}
                 scene.0_wer_lmp_group_1_all_on
              {% elif last == 'scene.0_wer_lmp_group_1_all_on' %}
                 scene.0_wer_lmp_group_2_all_50
              {% else %}
                 scene.0_wer_lmp_group_0_all_off
              {% endif %}

this however does not work for me, not with the toggle anyway, if you used a if else with scenes it would probably work.

this is why :

  • you turn of the lights with the all on scene, this is now the last scene.
  • you wait
  • later you go to the buttons, and press this one by accident (there are 3 next to each other)
  • The lights Toggle off, but the last scene called is still the 1_all_on
  • you quickly press again, becaus you didnt want it to go off. After the off call it should always go to the 1_all_on, but in this case it will run the 2_all_50

for now im sticking with this, until i have everything in the repeat in a script:

alias: TestInoPlafond
description: ""
trigger:
  - platform: state
    entity_id:
      - input_boolean.ino_helper_bool
    from:
      - "on"
      - "off"
    to:
      - "off"
      - "on"
condition: null
action:
  - alias: Default Action
    if:
      - condition: template
        value_template: "{{ state_attr('light.0_wer_lmp_group', 'brightness') >1 }}"
    then:
      - service: scene.turn_on
        metadata: {}
        target:
          entity_id: scene.0_wer_lmp_group_0_all_off
    else:
      - repeat:
          for_each:
            - scene: scene.0_wer_lmp_group_1_all_on
            - scene: scene.0_wer_lmp_group_2_all_50
          sequence:
            - service: scene.turn_on
              metadata: {}
              target:
                entity_id: "{{ repeat.item.scene }}"
            - if:
                - condition: template
                  value_template: "{{repeat.last == false}}"
              then:
                - wait_for_trigger:
                    - platform: state
                      entity_id:
                        - input_boolean.ino_helper_bool
                      from:
                        - "on"
                        - "off"
                      to:
                        - "off"
                        - "on"
                  timeout:
                    seconds: 5
                  continue_on_timeout: false
mode: single

why?
to explain the different parts (including the repeat.last == false)

any press of the button will evaluate the light, if it is on turn it off, and ends the script.

If the lights of the group are all off, start the scene changing script and switch between all selected scenes in order (you can add as many as you want withouh a problem)
(This will be fun in the next bit of code ill show :wink: )

The scene changing script wil:
Start the first selected scene, then, if not in the last scene, it will wait 5 seconds for a new press.
if a new press is given go to the next scene (continue until the last scene is reached.

On the Last scene the

            - if:
                - condition: template
                  value_template: "{{repeat.last == false}}"

repeat.last will be true, so this condition will not execute and the wait for the trigger will not execute, thus exiting this script. A new press will start the script from scratch and thus first turn on the light and then repeat the scene switching.

A big advantage, especially once I put the whole repeat thing in a script, is that I can do the following:

alias: Test2
description: ""
trigger:
  - platform: state
    entity_id:
      - input_boolean.ino_helper_bool
    from:
      - "on"
      - "off"
    to:
      - "off"
      - "on"
condition: null
action:
  - alias: Default Action
    if:
      - condition: template
        value_template: "{{ state_attr('light.0_wer_lmp_group', 'brightness') >1 }}"
    then:
      - service: scene.turn_on
        metadata: {}
        target:
          entity_id: scene.0_wer_lmp_group_0_all_off
    else:
      - choose:
          - conditions:
              - condition: sun
                after: sunset
                before: sunrise
            sequence:
              - repeat:
                alias: evening scenes
                  for_each:
                    - scene: scene.0_wer_lmp_group_3_evening_one
                    - scene: scene.0_wer_lmp_group_4_evening_two
                    - scene: scene.0_wer_lmp_group_5_evening_three
                    - scene: scene.0_wer_lmp_group_6_evening_four                    
                  sequence:
                    - service: scene.turn_on
                      metadata: {}
                      target:
                        entity_id: "{{ repeat.item.scene }}"
                    - if:
                        - condition: template
                          value_template: "{{repeat.last == false}}"
                      then:
                        - wait_for_trigger:
                            - platform: state
                              entity_id:
                                - input_boolean.ino_helper_bool
                              from:
                                - "on"
                                - "off"
                              to:
                                - "off"
                                - "on"
                          timeout:
                            seconds: 5
                          continue_on_timeout: false
        default:
          - repeat:
            alias: daytime scenes
              for_each:
                - scene: scene.0_wer_lmp_group_1_all_on
                - scene: scene.0_wer_lmp_group_2_all_50
              sequence:
                - service: scene.turn_on
                  metadata: {}
                  target:
                    entity_id: "{{ repeat.item.scene }}"
                - if:
                    - condition: template
                      value_template: "{{repeat.last == false}}"
                  then:
                    - wait_for_trigger:
                        - platform: state
                          entity_id:
                            - input_boolean.ino_helper_bool
                          from:
                            - "on"
                            - "off"
                          to:
                            - "off"
                            - "on"
                      timeout:
                        seconds: 5
                      continue_on_timeout: false
mode: single

easily switch between the scenes i want to activate! with a simple choose condition i can have multiple scene options for evening, morning, during the day, when the wife is not home etc. etc.

it really requires the sequence to be put in a script to keep it manageable, but i will work on that!

it even looks okay on the gui editor :smiley:

ah… i had presumed (and i thought your original code also did) that if you waited more than 5 seconds then you are no longer cycling… but that the next press was considered the “first” and then follows this rule:

at what point does the press become “first” again? or is there really no such thing as “first” and you’re always doing the cycle?

if you don’t want to do the toggle and want to use the scene, you can do this:

        - service: scene.turn_on
          target:
            entity_id: >
              {% if states('light.0_wer_lmp_group') == 'on') %}
                 scene.0_wer_lmp_group_0_all_off
              {% else %}
                 scene.0_wer_lmp_group_0_all_on
              {% endif %}

that’s effectively the same as toggle, but using your scenes, which then make the rest of the jinja format work.

ok, i understand why you want the ==repeat.last bit. there are a bunch of easier ways to handle that. e.g. having this part vary depending on time of day or whatever:

        {% set entities = ['scene.0_wer_lmp_group_0_all_off', 'scene.0_wer_lmp_group_2_all_50', 'scene.0_wer_lmp_group_1_all_on'] %}

however there’s always an enless number of ways to optimize code it seem… prob enough for now…

cheers!

1 Like