Code Reuse in YAML or Calling Multiple Scenes at the same time

I am just configuring scenes, where previously I used many automations.

I am running into some trouble through due to code bloat.

For example, I have 4 different scenes.

  • Playstation
  • Apple TV
  • Nintento Switch
  • VR PC

Each of these scenes turns off the basement lights, turns on the receiver, turns on the TV, activates the Hue Backlights and sets the color.

The problem is, its a huge amount of YAML to to this, when each scene only differs in that for the Apple TV and VR PC, I use Wake On Lan (Nintendo and Playstation don’t support it) and set the HDMI input on the receiver to the appropriate input.

I keep repeating all the exact same steps in the scene. Since you can no longer call a scene within a scene, how do I accomplish basically the following as a single task?

  • Set scene for Media Display
  • Set second scene for HDMI selection and Wake on Lan if appropriate

Have you considered using a script instead of a scene? A script allows you to employ templates and to pass variables to it thereby making it more flexible than a scene.

I normally use scripts and automations, but I had never used scenes before, so I thought I would give it a try.

It feels really clunky so far though, not sure its the best approach.

But I get what you are saying, write a script that calls multiple scenes.

Actually I’m suggesting to dispense with the scenes altogether but feel free to mix 'n match whichever way you feel is best.

I might just do that.

Though, it begs the question - what are scenes for then?

Full Disclosure: I use scripts instead of scenes.

Scenes are convenient because they simplify the process of specifying which entities do (very) specific things. Here’s an example of a scene in the documentation:

scene:
  - name: Romantic
    icon: "mdi:flower-tulip"
    entities:
      light.tv_back_light: "on"
      light.ceiling:
        state: "on"
        brightness: 200
        color_mode: "xy"
        xy_color: [0.33, 0.66]

It’s quite concise. If you try making the equivalent in a script it won’t be nearly as compact. As long as the values are static, the scene is neater.

However, the moment you need to make the values dynamic, where the value is computed by a template, or flow-control decisions must be made about which actions are executed, then you’re beyond the abilities of a scene.

1 Like

This seems to work well.

Create a “base” scene called basement_media. This turns off all the lights, turns on the Hue Backlights, turns on the TV etc. Then I call the Playstation scene which tells the receiver which HDMI port to use (this also turns the receiver on).

Then, in my scripts.yaml I have:

media_test:
  alias: Test Scenes
  sequence:
  - scene: scene.basement_media
  - scene: scene.playstation

I think this will do the trick.

FWIW, part of the reason I am going through all this, is I want better compartmentalization.

I have a Hue Sync Box on order, and I will be able to adjust only the scene.basement_media component to turn it on with my other scenes. Meaning I have compartmentalized my code into discrete blocks, instead of sprawl. :slight_smile:

1 Like