Turn on scene only if no other scene is active

Hey guys, I just started with Home Assistant. For now I made 2 scenes. One for when watching a movie, and one for just the evening chill. The evening chill scene turns on automatically at sunset. The problem however, is it also does this when i am watching a movie and the movie scene is on. This is annoying. Therefore I would like to edit the automation such that the evening chill scene only turns on if the movie scene is not on.

I read that the state of a scene is always “Scening”, so this can not be used as a condition.

Do you guys have any idea how I can accomplish this?

Thanks in advance!

As you have already discovered, a scene’s state is simply scening. Effectively, it is stateless so you cannot tell if a scene has been applied (i.e. “on”) or not.

You’ll have to keep track of that using another entity, such as an input_boolean, turning it on whenever you activate the scene and turning it off when appropriate (you’ll have to determine what constitutes a scene not being in effect).

Alright, thanks a lot for the info. I will dig into that tomorrow!

Here is a suggestion that employs a Template Switch. This is modeled on something I use to provide an “artificial state” for a scene.

The idea is to create two Template Switches, one for each scene. The result will be:

  1. switch.movie
  2. switch.chill

How it works:

  • When you turn on switch.movie, it will store the current state of various entities (that you have predefined; basically they are the entities in your existing movie scene) in a temporary scene called scene.before_movie and then activate your existing movie scene. (The temporary scene is lost after a restart.)
  • When you turn off switch.movie, it will turn on scene.before_movie, effectively restoring the state of the predefined entities.

Each Template Switch’s state is stored by an accompanying input_boolean. You must create the following two input_booleans:

  1. input_boolean.movie
  2. input_boolean.chill

So when you want to activate the chill scene, you turn on switch.chill. It has a state value so you know when it it on. Now when you compose your automation, you can easily determine if either the movie or chill scenes are activated by examining the current state of switch.movie and switch.chill, respectively. Example:

trigger:
- platform: state
  entity_id: sun.sun
  to: below_horizon
action:
- choose:
  - conditions: "{{ is_state('switch.movie', 'off') }}"
    sequence:
    - service: scene.turn_on
      target:
        entity_id: scene.chill

In the following example, the entities you see listed in snapshot_entities should match the ones used in your scene. They are the entities whose current states will be stored to scene.before_movie and then restored to those states when the switch is turned off.

switch:
  - platform: template
    switches:
      movie:
        friendly_name: Movie Scene
        value_template: "{{ states('input_boolean.movie') }}"
        turn_on:
          - service: input_boolean.turn_on
            target:
              entity_id: input_boolean.movie
          - service: scene.create
            data:
              scene_id: before_movie
              snapshot_entities:
                - climate.thermostat
                - light.family_room
                - switch.tv_light
          - service: scene.turn_on
            target:
              entity_id: scene.movie
        turn_off:
          - service: input_boolean.turn_off
            target:
              entity_id: input_boolean.movie
          - service: scene.turn_on
            target:
              entity_id: scene.before_movie

      chill:
        friendly_name: Chill Scene
        value_template: "{{ states('input_boolean.chill') }}"
        turn_on:
          - service: input_boolean.turn_on
            target:
              entity_id: input_boolean.chill
          - service: scene.create
            data:
              scene_id: before_chill
              snapshot_entities:
                - light.kitchen
                - light.family_room
                - light.bedroom
          - service: scene.turn_on
            target:
              entity_id: scene.chill
        turn_off:
          - service: input_boolean.turn_off
            target:
              entity_id: input_boolean.chill
          - service: scene.turn_on
            target:
              entity_id: scene.before_chill

The way I keep track of which scene is applied is by using an input select.

If I want to apply a scene I change the input select and this automation applies the scene:

- id: ea79b207-9172-48fd-be8d-fc74ca0479ad
  alias: 'Deck Scene Select'
  trigger:
    platform: state
    entity_id: input_select.deck_scene
  action:
  - service: homeassistant.turn_on
    data:
      entity_id: >
        {% if states('input_select.deck_scene') == 'Off' %}
          scene.deck_off
        {% elif states('input_select.deck_scene') == 'Automatic' %}
          scene.deck_automatic
        {% elif states('input_select.deck_scene') == 'Bright' %}
          scene.deck_bright
        {% elif states('input_select.deck_scene') == 'Forest' %}
          scene.deck_forest
        {% elif states('input_select.deck_scene') == 'Imperial' %}
          scene.deck_imperial
        {% elif states('input_select.deck_scene') == 'Night Light' %}
          scene.deck_night_light
        {% elif states('input_select.deck_scene') == 'Party' %}
          script.deck_party
        {% elif states('input_select.deck_scene') == 'Sky' %}
          scene.deck_sky
        {% elif states('input_select.deck_scene') == 'Under Water' %}
          scene.deck_under_water
        {% elif states('input_select.deck_scene') == 'Xmas' %}
          scene.deck_xmas
        {% elif states('input_select.deck_scene') == 'Zen' %}
          scene.deck_zen
        {% else %}
          scene.deck_automatic
        {% endif %}

I believe you can reduce it to this (it will fail only if the input_select contains an option for which there is no accompanying scene):

- id: ea79b207-9172-48fd-be8d-fc74ca0479ad
  alias: 'Deck Scene Select'
  trigger:
    platform: state
    entity_id: input_select.deck_scene
  action:
  - service: homeassistant.turn_on
    data:
      entity_id: "scene.deck_{{ states('input_select.deck_scene') | replace(' ', '_') | lower }}"

Your suggestion is a clean and easy way to keep track of one scene at a time.

The advantage of my admittedly more complex suggestion is that it can handle multiple concurrent scenes (i.e. more than one scene can be active at the same time). In fact, if scenes have entities in common, this arrangement allows you to “stack” scenes.

For example, let’s say two scenes have light.tv in common. One scene is handled by a Template Switch called switch.movie and the other scene by another Template switch named switch.doorbell.

  • Turning on switch.movie sets light.tv to 25% and deep blue.
  • Someone rings the doorbell which turns on switch.doorbell which sets light.tv to 75% warm white and pauses media_player.tv (if currently playing).
  • After you are done with the visitor, you turn off switch.doorbell. That restores entities to their pre-doorbell states so light.tv goes back to 25% and deep blue and media_player.tv plays again.

Throughout all of this, switch.movie remained on. Turning it off will restore entities to their pre-movie states.

2 Likes

Dude, this is great! It took me some time to comprehend everything you said and putting it in my files the right way. But eventually it is working, exactly as you said!

I actually do that for other areas but if you look closely, one of the scenes is actually a script (“Party”), hence the long version.