Dynamic scene for storing state not working as expected

I am using an automation to flash a light when a door contact sensor is being triggered. To store the settings of the light I am using a dynamic scene at the start of the automation, I flash the light and turn on the previously stored dynamic scene:

alias: door_sensor-flash_open-zolder
description: ''
trigger:
  - type: opened
    platform: device
    device_id: 2088ea3d715545730f75779f1c5b2374
    entity_id: binary_sensor.deursensor_1_contact
    domain: binary_sensor
    id: zbsensor
condition: []
action:
  - service: scene.create
    data:
      scene_id: temp_paneel_werkkamer_flash
      snapshot_entities:
        - light.paneel_werkkamer
  - device_id: fb16cb1ee5304cb9afd933af0cfa17a1
    domain: light
    entity_id: light.paneel_werkkamer
    type: flash
  - service: scene.turn_on
    data: {}
    target:
      entity_id: scene.temp_paneel_werkkamer_flash
mode: single

The scene is created by the automation, I can see in under the scenes menu. However, the light is not restored to it’s previous state and always stays on. The reason I am storing the state is because the light stays on if it is done flashing, even if it were off before.

When I try to apply the state via the web interface via the scenes menu nothing seems to happen. Under development tools it seems the contain the right light.

So it seems like the dynamic scene is not stored or applied correctly. I am using the example from the scenes docs so I am assuming my configuration is correct. My logs seem to be clean, no errors around the time the automation is being triggered.

Anybody got any ideas?

2 Likes

As far as I know you need to do two stores and restores to get this done. First you need to store the entity’s state (on or off) and then the entity’s attributes, and than -if your thing is done- restore the attributes and then the state.
The caveat of this is that you only can grab the attributes when the light is on, so you need to switch the light on first to do so.

Here is the deal:

  1. Snapshot the entity’s state
  2. Switch the entity on
  3. Snapshot the entity’s attributes
  4. Restore the entity’s state (so that when it was on it stay’s on, and when it was off it stays off).
  5. Do you thing with the light

Between steps 1 to 5 you need to give HA/NR some time (100ms or so, dependent on the speed of your system).

Here is my applied flow:

Here is the flow in JSON:

[{"id":"a05100b13db18d8e","type":"group","z":"11a02663.08c012","name":"TrapKastDeur Signaal functie WK_4","style":{"stroke":"#0070c0","fill":"#ffffbf","label":true,"color":"#0070c0"},"nodes":["9e111ba1c52a0e2c","7001965e84de2514","8172042c97344d11","f42eb1fc196943a8","785e9e38f04e37bf","5a2b6e4cf6737b58","681852033a8f630f","2e2ea2222f92fd3a","3e0fae0941862951","55e9f93b4ceab156","81f4eb65eebc4e58","95f8d302b8e2c5b8","a4bb90bd0cdb2aa4","e7c35f1d42b7dfc6","7b53683312cd5b4b","b8acae84741d3800","8f0cc76da65da7d8"],"x":3114,"y":1419,"w":982,"h":402},{"id":"9e111ba1c52a0e2c","type":"link in","z":"11a02663.08c012","g":"a05100b13db18d8e","name":"","links":["e67359e75d0dde6d"],"x":3155,"y":1460,"wires":[["81f4eb65eebc4e58","95f8d302b8e2c5b8"]]},{"id":"7001965e84de2514","type":"delay","z":"11a02663.08c012","g":"a05100b13db18d8e","name":"30 sec","pauseType":"delay","timeout":"30","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":3260,"y":1600,"wires":[["8172042c97344d11"]]},{"id":"8172042c97344d11","type":"api-call-service","z":"11a02663.08c012","g":"a05100b13db18d8e","name":"Snapshot woonkamer_4 state","server":"6182da52.101a64","version":3,"debugenabled":false,"service_domain":"scene","service":"create","entityId":"","data":"{\"scene_id\":\"woonkamer_4_state_before\",\"snapshot_entities\":[\"light.woonkamer_4\"]}","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"first","x":3480,"y":1600,"wires":[["8f0cc76da65da7d8"]]},{"id":"f42eb1fc196943a8","type":"api-call-service","z":"11a02663.08c012","g":"a05100b13db18d8e","name":"Woonkamer_4 ON","server":"6182da52.101a64","version":3,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.woonkamer_4","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":3440,"y":1660,"wires":[["785e9e38f04e37bf"]]},{"id":"785e9e38f04e37bf","type":"delay","z":"11a02663.08c012","g":"a05100b13db18d8e","name":"","pauseType":"delay","timeout":"100","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":3620,"y":1660,"wires":[["5a2b6e4cf6737b58"]]},{"id":"5a2b6e4cf6737b58","type":"api-call-service","z":"11a02663.08c012","g":"a05100b13db18d8e","name":"Snapshot woonkamer_4 attrib","server":"6182da52.101a64","version":3,"debugenabled":false,"service_domain":"scene","service":"create","entityId":"","data":"{\"scene_id\":\"woonkamer_4_attrib_before\",\"snapshot_entities\":[\"light.woonkamer_4\"]}","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"first","x":3480,"y":1720,"wires":[["681852033a8f630f"]]},{"id":"681852033a8f630f","type":"delay","z":"11a02663.08c012","g":"a05100b13db18d8e","name":"","pauseType":"delay","timeout":"100","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":3700,"y":1720,"wires":[["2e2ea2222f92fd3a"]]},{"id":"2e2ea2222f92fd3a","type":"api-call-service","z":"11a02663.08c012","g":"a05100b13db18d8e","name":"Restore Woonkamer_4 State","server":"6182da52.101a64","version":3,"debugenabled":false,"service_domain":"scene","service":"turn_on","entityId":"scene.woonkamer_4_state_before","data":"","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":3470,"y":1780,"wires":[["3e0fae0941862951"]]},{"id":"3e0fae0941862951","type":"delay","z":"11a02663.08c012","g":"a05100b13db18d8e","name":"","pauseType":"delay","timeout":"100","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":3680,"y":1780,"wires":[["55e9f93b4ceab156"]]},{"id":"55e9f93b4ceab156","type":"api-call-service","z":"11a02663.08c012","g":"a05100b13db18d8e","name":"Change woonkamer_4 to PURPLE","server":"6182da52.101a64","version":3,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.woonkamer_4","data":"{\"rgb_color\":[255,0,255],\"brightness\":254}","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":3910,"y":1780,"wires":[[]]},{"id":"81f4eb65eebc4e58","type":"change","z":"11a02663.08c012","g":"a05100b13db18d8e","name":"Reset Timer","rules":[{"t":"set","p":"reset","pt":"msg","to":"1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":3280,"y":1520,"wires":[["7001965e84de2514"]]},{"id":"95f8d302b8e2c5b8","type":"delay","z":"11a02663.08c012","g":"a05100b13db18d8e","name":"","pauseType":"delay","timeout":"500","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":3280,"y":1460,"wires":[["a4bb90bd0cdb2aa4"]]},{"id":"a4bb90bd0cdb2aa4","type":"api-call-service","z":"11a02663.08c012","g":"a05100b13db18d8e","name":"Restore Woonkamer_4 attrib","server":"6182da52.101a64","version":3,"debugenabled":false,"service_domain":"scene","service":"turn_on","entityId":"scene.woonkamer_4_attrib_before","data":"","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":3530,"y":1460,"wires":[["b8acae84741d3800"]]},{"id":"e7c35f1d42b7dfc6","type":"api-call-service","z":"11a02663.08c012","g":"a05100b13db18d8e","name":"Restore Woonkamer_4 State","server":"6182da52.101a64","version":3,"debugenabled":false,"service_domain":"scene","service":"turn_on","entityId":"scene.woonkamer_4_state_before","data":"","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":3950,"y":1460,"wires":[[]]},{"id":"7b53683312cd5b4b","type":"link in","z":"11a02663.08c012","g":"a05100b13db18d8e","name":"","links":["619f73dd7a895e89"],"x":3155,"y":1600,"wires":[["7001965e84de2514"]]},{"id":"b8acae84741d3800","type":"delay","z":"11a02663.08c012","g":"a05100b13db18d8e","name":"","pauseType":"delay","timeout":"100","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":3740,"y":1460,"wires":[["e7c35f1d42b7dfc6"]]},{"id":"8f0cc76da65da7d8","type":"delay","z":"11a02663.08c012","g":"a05100b13db18d8e","name":"","pauseType":"delay","timeout":"100","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":3710,"y":1600,"wires":[["f42eb1fc196943a8"]]},{"id":"6182da52.101a64","type":"server","name":"Wolk 9 Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"10"}]

I hope this will solve your challenge.

1 Like

Thank you for your reply!

You’re saying you need to snapshot the entity’s attributes after I turn it on. But how about if the entity if off (no light) at the start and it should be switches off again at the end of the automation? I don’t turn it on at the start so I can’t store the attributes, right?

1 Like

Nope, the attributes, as well as the state are safed in the scene, there is no need to do this twice. Must be something different, that is not working.

You might want to check the automation history under Settings > Automations & Scenes > door_sensor-flash_open-zolder > the clock with the arrow. Let the automation run and follow the exact steps in the history. You should be able to see where it goes sideways.

I’ve a similar scene set, the onlx difference in my case is, I’m restoring the scene in a second automation. Like door opens > save scene > light flash and in the second one restore the state. In my case thios is necessary because some other stuff the automation does. :slight_smile:

EDIT: maybe the note from @Martin_de_Bes could be something. Have you tried setting a small delay between the storing of the scene and the reactivating?

1 Like

Yeah so unfortunately if you create the scene when the light is off, the attributes won’t get saved. And then if your automation messes with the attributes in any way, restoring the scene won’t do what you want it to. And yes that means your light has to turn on first regardless, which may look a bit jarring. If the goal is to softly do something in the bedroom, prepare to be woken up by a full light, no way around it … ask me how I know.

You have to : save to a scene 1, turn it on, save to a scene 2, do your automation, restore scene 2, restore scene 1.
That’s sadly the only way to get scenes working the way you likely want them to.

That’s something that really could be part of a WTH month I think, it’s weird and annoying behavior and makes automations way more complex than they should be. You could probably move most of that logic out into the scene save function directly

1 Like