Turning on a scene with a dynamic entity_id in a script

Hi,

I’m trying to make a script (my first) that remembers the current scene as well as the previous scene. Once that is done it should set the scene.

Setting the input_text variables works fine. As soon as I try to save the part where I turn on the scene, I get an error: Invalid config for [script]: not a valid value for dictionary value @ data['sequence'][0]['target']['entity_id']. Got None. (See ...).

script:
  update_hallway_scene:
    fields:
      scene_id:
        description: "the new scene ID"
    mode: queued
    sequence:
      - service: input_text.set_value
        target:
          entity_id: input_text.hallway_scene_previous
        data_template:
          value: "{{ states.input_text.hallway_scene_current.state }}"
      - delay:
          milliseconds: 1
      - service: input_text.set_value
        target:
          entity_id: input_text.hallway_scene_current
        data_template:
          value: "{{ scene_id }}"
      - service: scene.turn_on
        target:
          entity_id: >
            {% 'scene.' ~ scene_id %}

I’ve been searching for a while and tried several things (e.g. entity_id: "{{ scene_id }}") but feel a bit stuck. Some help would be much appreciated.

- service: scene.turn_on
  data:
    entity_id: "{{ 'scene.' ~ scene_id }}"

It’s better to use states('input_text.xxx') notation than states.input_text.xxx, as explained here.
Why do you have a delay between setting the state of the previous and current input_text?

The target option is a recently introduced convention that is, as far as I’ve seen, undocumented. It appears in a few examples in the documentation but there’s no guidance for its usage. If you’ve seen it described anywhere, please let me know.

I suggest using the traditional way:

      - service: scene.turn_on
        data:
          entity_id: "scene.{{ scene_id }}"

… and while typing my response I was ninja’d by Burningstone …


EDIT

I’ll just add that the entire script can dispense with target and become less verbose:

script:
  update_hallway_scene:
    fields:
      scene_id:
        description: "the new scene ID"
    mode: queued
    sequence:
      - service: input_text.set_value
        data:
          entity_id: input_text.hallway_scene_previous
          value: "{{ states.input_text.hallway_scene_current.state }}"
      - delay:
          milliseconds: 1
      - service: input_text.set_value
        data:
          entity_id: input_text.hallway_scene_current
          value: "{{ scene_id }}"
      - service: scene.turn_on
        data:
          entity_id: "scene.{{ scene_id }}"

FWIW, data_template was deprecated in favor of data several versions ago.

1 Like

Thanks a lot for the quick answer. Both solutions work :grinning:

I was using target because I saw it being used here: https://www.home-assistant.io/docs/scripts/service-calls/#use-templates-to-determine-the-attributes

target was first seen when blueprints were introduced and now it’s trickling into the documentation’s examples for automations that have nothing to do with blueprints. Beyond blueprints, there has been no explanation how it’s supposed to be used in an automation (or even why). I assume it’s part of an improvement but it’s unclear what that might be. Automations continue to work well without employing target and are less verbose as well.

1 Like

See this documentation PR:

Here’s my best guess as to how the new convention works (there has been no formal announcement of it yet; perhaps in the upcoming March release):

In the past, you would use data to supply a service call with all the information it needs:

- service: light.turn_on
  data:
    entity_id: light.kitchen
    brightness: 128

It appears that now target will be used to specify what the service call should use and optionally data for how it should do it.

- service: light.turn_on
  target:
    entity_id: light.kitchen
  data:
    brightness: 128

I don’t know why this is being done; rather than speculate I’ll wait for this new convention to be officially announced and/or documented.

FYI template support has been added for target: in v2021.3.