How to turn a scene off? No scene.turn_off

Hi,
I’ve managed to create it so that a scene is activated when a door is opened during a certain time, however I can’t seem to get it to work to turn the lights off when the door is closed. This is my current code:

alias: Bedroom Door Closed
  description: Night closing for bathroom
  trigger:
  - entity_id: binary_sensor.bedroom_door_contact
    from: 'on'
    platform: state
    to: 'off'
  condition:
  - after: '16:00:00'
    condition: time
  - condition: and
    conditions:
    - before: sunrise
      before_offset: '+20:00'
      condition: sun
  action:
  - data:
      entity_id: scene.bathroom_bedroom
    service: homeassistant.turn_off

I use scene.turn_on as the service to activate the scene, however can’t find what to use to turn off. I tried homeassistant.turn_off, but no joy.

Any help appreciated.

1 Like

In order to turn off a scene, Home Assistant would have to maintain a record of what state everything is in before it turns on the scene. Turning off a scene would make Home Assistant use that record to restore everything back to its previous state.

This ‘set-restore’ behavior isn’t currently available in Home Assistant. Scenes only support ‘set’ behavior. That’s why there’s no scene.turn_off service.

1 Like

Many thanks for your reply @123

I’ve tried a different route of getting HA to turn off the 3 lights that would be on, however not having much joy. It only seems to be turning the bathroom light off when the door is closed. Any idea where I have gone wrong here?

description: Night closing for bathroom
  trigger:
  - entity_id: binary_sensor.bedroom_door_contact
    from: 'on'
    platform: state
    to: 'off'
  condition:
  - after: '16:00:00'
    condition: time
  - condition: and
    conditions:
    - before: sunrise
      before_offset: '+20:00'
      condition: sun
  action:
  - data:
      entity_id: 
        light.landing_dimmable_1
        light.top_landing_2
        light.bathroom_light_3
    service: light.turn_off

There are two new services that I have not tried yet: scene.create and scene.apply.

Dreaming in technicolor: Maybe those new services can be used to capture the current state of desired devices and use that later when the time comes to restore their states. :man_shrugging:

FYI: pnbruckner developed a python_script that saves/restores the state of lights.

2 Likes

Need to make them a list.

  action:
  - service: light.turn_off
    entity_id: 
    - light.landing_dimmable_1
    - light.top_landing_2
    - light.bathroom_light_3

or…

  action:
  - service: light.turn_off
    entity_id: light.landing_dimmable_1, light.top_landing_2, light.bathroom_light_3

(removed the data: as it’s not needed here.)

2 Likes

Worked a charm, thank you!

Those services do work as you described based on what I read. Using those service calls with templates should make it easy to capture the current state of lights to be applied later.
https://www.home-assistant.io/integrations/scene/#creating-scenes-on-the-fly

What I’m failing to understand is the purported benefit of the new services. In the example showing how to ‘create a scene on the fly’, the automation lists all the entities and their desired states. In other words, the scene is defined within the automation’s action.

What is the advantage of that over defining the scene the traditional way? Then just calling it in the automation?

Let’s say you have an automation that turns the lights off in your living room when playing media. You could capture the state before turning the lights off and restore it when you stop playing media. I think that’s one potential use case at least.

How would you capture the current state? The documentation’s example doesn’t show how that can be done, it explicitly defines the entity’s state.

  action:
    service: scene.create
    data:
      scene_id: my_scene
      entities:
        light.tv_back_light:
          state: on
          brightness: 100
        light.ceiling: off
        media_player.sony_bravia_tv:
          state: on
          source: HDMI 1

Pardon my thick skull but I don’t see how it can capture current states unless templates can be used sort of like this:

  action:
    service: scene.create
    data_template:
      scene_id: my_scene
      entities:
        light.tv_back_light:
          state: "{{ states('light.tv_back_light')}}"
          brightness: "{{ state_attr('light.tv_back_light', 'brightness')}}"
        light.ceiling: "{{ states('light.ceiling')}}"
        media_player.sony_bravia_tv:
          state: "{{ states('media_player.sony_bravia_tv')}}"
          source: "{{ state_attr('media_player.sony_bravia_tv', 'source')}}"

Now scene.my_scene contains the current states of the two lights and media_player. If I now change the states of the lights or media_player, I can use scene.my_scene afterwards to restore them to their previous states.

However, this assume templates can be used and I’m uncertain if that’s true.


EDIT
Correction. Replaced data with data_template.

With data_template I’d imagine that would be possible (that’s what I ultimately meant). But I also don’t know for sure.

I’m pleased to report that it works. With some pre-planning, it’s possible to store entity states as a new (temporary) scene and then restore it afterwards (but before the next restart because temporary scenes don’t survive a restart).

Here’s the test. I’m using an input_boolean to trigger the automations just for convenience’s sake.

- alias: test1
  trigger:
    platform: state
    entity_id: input_boolean.toggler
    to: 'on'
  action:
    service: scene.create
    data_template:
      scene_id: test1
      entities:
        light.family:
          state: "{{ states('light.family')}}"
          brightness: "{{ state_attr('light.family', 'brightness')}}"
        light.kitchen: 
          state: "{{ states('light.kitchen')}}"
          brightness: "{{ state_attr('light.kitchen', 'brightness')}}"

- alias: test2
  trigger:
    platform: state
    entity_id: input_boolean.toggler
    to: 'off'
  action:
    service: scene.turn_on
    entity_id: scene.test1

Initial conditions:

  • Family room light is at 25%.
  • Kitchen light is off.

Test procedure:

  1. Turn on the input_boolean and the automation stores the current states of the two lights in a temporary scene (called scene.test1).
  2. Manually turn off the family room light and turn on the kitchen light.
  3. Turn off the input_boolean and the automation turns on scene.test1. The two lights are restored to their previous states (family room light is back on at 25% and the kitchen light is off).

This is a very useful feature and it seems like it hasn’t received the attention it deserves. The need to ‘set-restore’ states comes up often and this does the trick.

5 Likes

Awesome, glad to hear it. I hadn’t gotten around to testing it out, so I’m glad to see it works. It got some pretty positive reactions during the State of the Union stream, so there’s that at least.

Only issue I found is with your input_boolean. Entity ID should be input_boolean.flippy_boi

Well I do have one called flipper

Excellent. :stuck_out_tongue:

Wasn’t this one is the things they talked about in the state of the union in November?

Yeah it was

I thought so. It certainly seems very useful. Do you know if you can ‘overwrite’ a scene? I can see it being very useful to create a scene based on current conditions and then reverting to that state later on, but if I do it more than once, then that scene will already exist.

Create another scene with the lights off…then activate that scene.

Not the same. I don’t just want to turn the lights on and off I want to capture the state of an area, like lights on/off, their brightness, media volume, etc. to be able to set it back to that state later.

Eg. One automation captures the state and sets everything to something else. Then a separate automation sets everything back the way it was when it was captured using that scene. The next time that first automation runs, I would want that scene to be overwritten. Maybe I can delete it after it’s applied back?