Scenes - saving light states before applying a scene and then a scene (and then restore light states)

I am a new user of scenes and trying to fit it into our “current way of living”, were the family typically pushes wall-switches to turn on the lights. So I have started to create scenes that can be manually applied (see image below).

In this setup I created scripts that saves the previous light settings before applying a pre-configured scene, ie so tapping “All on” - will save the current setup (scene.create with snapshot_entities) and apply the “All on” screne turn on all lights. Tapping “Previous” will return to the previous setting (scene.turn_on with the created scene.before). Everyone is :star_struck:

image

But…that only works if we move in and out of one scene at a time…so if somebody first taps “All on” (maybe by mistake) and then “Movie”, tapping “Previous” will return to the “All on” scene. Most are :tired_face:

So (maybe) I am looking for ideas to save the light states before entering scenes (or maybe I need to look for some other ways). Any ideas or comments are welcome on how to accomplish this?

1 Like

It’s tricky to code what people are thinking.

Is this a case of trying to overcome a problem that doesn’t exist given most people will probably remember which of the 4 choices was the one they were trying to return to?

I did something quite similar. I had 5 scenes (ranging from 'all off" to “all on”) that were auto-selected based on an outside lux sensor. I also had buttons to manually select each scene (essentially an override to the lux-based automation). I then created a button called “reset” which manually triggered the automation to re-select the ‘correct’ scene - useful after a manual selection, right? However, I quickly realised that no-one ever used that button because they were either happy with their manual selection or they simply set a new one that they were happy with.

Sorry for my unclear description.

Here is a scenario;
Wife and kids mostly control our lights using the wall-switches (having Shelly dimmers behind them) and not using any scenes. Later we might decide to watch a movie, but as we couldn’t find the remote control I apply the “All on” scene to look for it. After finding it I turn on the Movie scene.
After the movie finished I would like to go back to the light state that we had before we started to look for the remote control.

So maybe its a way of trying to bridge between wall-switch based light control and scene based light control.

…perhaps only use scene.create when pushing the “Movie” scene button so no other scene can effect it?

Hmm…not sure I understand how that would solve it…

Then I wouldn’t not be able to revert to the “previous” state (of lights) when activiating other scenes like the “All on” scene.

…and thanks for taking the time.

Sorry, I thought that example was the scenario, not a scenario,

You need to find a unique real-world identifier that is unique to wanting to ignore the scene_create. For example, would it always be when the scene is only active for (say) 3 minutes? If so, you can set a 3 min timer when each scene is selected and have the script only scene_create if the timer isn’t running when the button is pushed.

1 Like

Make the “previous” button call a “home” state not an actual stored “previous” state. When previous is pressed you load the state you created called “home” or default. Something like that. I have a light that I call a flicker state when a camera sees someone at the door, then it calls a default, home state after a few seconds to put things back to normal or default.

1 Like

Thanks - your comments maybe me think a bit…and maybe this is exactly what you are suggesting but I couldn’t grasp… :smiley:

EDITED: This became my solution;

I created an input select like “scene_is_active” to indicate if a scene is applied or not + a timer how long the snapshot scene shall be consider valid to revert to.

  • When user taps to apply a scene it runs a script; to check if “scene_is_active” = FALSE, then sets “scene_is_active” to TRUE + do a scene.create snapshot + start a timer of 3h + applies the selected scene.

  • If the scripts finds out that “scene_is_active” = TRUE, then just applies selected the scene (ie. don’t update the saved snapshot scene).

  • When user taps “Previous” - I revert back to the “snapshot scene” + set “scen_is active” to FALSE + cancels the timer.

  • If the timer finshes I set “scene_is_active” = FALSE. (as after some 3h I could consider he “previous” settings are old…).

Sorry if unclear description, if anyone is interested I am happy to share more.

1 Like

Hi Nikno,

I was also looking for this kind of solution.
Can you share how you did it exactly?

Basically this:

  • I created the scenes I wanted.
  • I created a helper timer (l1_scene_control) to control how long it shall be possible to revert to light setting before applying a scene.
  • I created a help input select (l1_scene_active) to keep track if a scene is active or not.
  • I created an automation that sets l1_scene_active to FALSE when the timer (l1_scene_control) runs out.
  • I created scripts that activates each scene (and takes a snapshot of the current light setting) - see below.
  • I created a script that applies the light setting before applying a scene - see below.

The script yaml code to set a scene looks like this:

alias: L1 - Apply scene Movie
sequence:
  - if:
      - condition: state
        entity_id: input_select.l1_scene_active
        state: "FALSE"
    then:
      - service: input_select.select_option
        data:
          option: "TRUE"
        target:
          entity_id: input_select.l1_scene_active
      - service: scene.create
        data:
          scene_id: l1_before
          snapshot_entities:
            - light.bulb_l1_entrance_hallway
            - light.bulb_l1_office_desk_lamp
            - light.bulb_l1_window
            - switch.plug_l1_panthella_switch
      - service: timer.start
        data: {}
        target:
          entity_id: timer.l1_scene_control
      - service: scene.turn_on
        data: {}
        target:
          entity_id: scene.1f_movie
    else:
      - service: scene.turn_on
        data: {}
        target:
          entity_id: scene.1f_movie
mode: single
icon: mdi:lightbulb-auto

The script yaml code to apply light setting before scene was applied looks like this:

alias: L1 - Apply before scene
sequence:
  - service: scene.turn_on
    data: {}
    target:
      entity_id: scene.l1_before
  - service: input_select.select_option
    data:
      option: "FALSE"
    target:
      entity_id: input_select.l1_scene_active
  - service: timer.cancel
    data: {}
    target:
      entity_id: timer.l1_scene_control
mode: single
icon: mdi:lightbulb-auto
1 Like

Thanks a lot!
I combined your code with some of what I created already and it works nice.
The automation gets triggered when my android tv starts to play a movie. The only problem i’ve noticed is that the scene also gets triggered when the tv plays an intro or trailer in the menus.

1 Like

Maybe off topic for this thread, but potentially it might work having a condition based on which app is playing the media? I see that on when my NVIDIA Shield plays there is property called app_name on the media player which is populated with things like “Disney+”.

What the OP asked about could be done with stateful scenes for the most part. https://github.com/hugobloem/stateful_scenes

One can manipulate the stateful scene config entities to control whether the scene restores previous state, calls another scene for off, or just turns off. This method allows one to effectively chain scenes together in multiple ways.