Scene with dynamic (templated) entities parameter

I’m automating a “sleep mode”, where certain lights turn off when it’s activated.
For this I want to use a scene, called Sleeping.

I want the logic to be “always turn off switch.plug_1 (master bedroom) and turn off switch.plug_2 if there’s no guest in that room”. The guest/no guest is controlled by input_boolean.bed_2.
So the scene entities parameter should always include “switch.plug_1: false”, and only include “switch.plug_2:false” if “input_boolean.bed_2” is on (if bed_2 is off the switch should be left as is).

I’ve tried different variations of the code below but I can’t get it to work. What am I doing wrong?

  - name: Sleeping
      switch.plug_1: false
      {% if is_state("input_boolean.bed_2", "on") -%}switch.plug_2: false{%- else -%}{%- endif %}

You can’t template in every field. This is a field that is not templateable. Nothing in scenes is templateable. Try a script instead. Also a good rule of thumb: “If the field name or parent field name doesn’t have the word template, you can’t template there”.

example of a field name with the word template

value_template: "{{ ... }}"

example of a parent field name with the word template

  entity_id: "{{ ... }}"

I believe data_template is the only parent field that allows templating.

Thanks @petro, that explains a lot!
So I’ve broken it out to a script. Since I have multiple guest rooms, I tried putting them in order of condition - service - condition - service, but it seems to be working in serial, so if condition 2 fails, condition 3 will never be reached.

My next solution was to use services with templated entity id’s, like so:

        entity_id: >
          {% if is_state('input_boolean.bed_2', 'on') %}
          {% endif %}

With each bed/switch after another. This seems to be working, but when the input is “off”, it’s throwing a silent error in the logs with:

[homeassistant.components.homeassistant] homeassistant/turn_off cannot be called without entity_id

The error makes perfect sense, but I have no idea how to avoid it. Any clever way around this?

You’d have to break out each conditioned section into it’s own script and call the script. Or make a dummy feature to turn off, like an input boolean.

Thanks! Thats a great idea. As a dummy I can just turn off the input boolean that’s already off =)

- service: homeassistant.turn_off
        entity_id: >
          {% if is_state('input_boolean.bed_2', 'on') %}
          {% else %}
          {% endif %}

As I found this thread by Googling for a slightly different question, I’d just like to add my $0.02 here in case this helps someone else.

Yes, it is correct that static, saved scenes in HomeAssistant do not support templates. This, I’d imagine, could lead to unpredictability if not used carefully. Changes to entities requiring scene updates should be a rare occurrence and something that a user would be conscious of anyway, so it’s not a huge issue to be lacking any dynamics within a scene record.

However, when creating an on-the-fly scene, you actually CAN use templates - this is because service calls do accept templates (although, as of this writing, the visual editor will complain about using templates and force you into YAML mode, but that’s OK).

I have an “On Air” scene that I activate when I am on a call. For this scene, I want certain lights in the house to turn red and hold red until I de-activate the On Air mode (which I track with a boolean helper). If I alter the scene, I do not want to also remember to alter the automation which calls scene.create each time. It’s rare that I might want to change this, but I also want to make it simple when I do (and it has come up before!).

So, let’s use HomeAssistant to automate maintainence of HomeAssistant :slight_smile: My service call to scene.create looks like this:

service: scene.create
  scene_id: on_air_restore_state
  snapshot_entities: "{{ state_attr('scene.on_air', 'entity_id') }}"

When this automation is triggered, it will always snapshot whatever entites are in my scene.on_air. I have a separate automation set up for when i deactivate On Air mode which simply sets the scene scene.on_air_restore_state (although I really should combine these and use Trigger IDs to make the whole thing more modular!)

So, as you can see, if you are trying to create a scene on the fly from a list of entities, you can do it using templating in the service call rather than trying to define the scene with templates in it. This does have the caveat that if the list changes after the scene has been defined, it will not update within the scene; but for 99% of use cases this probably isn’t going to be an issue.