What am I missing: basic use of a variable in a script?

Ok, I’m a newby with HA, but have done quite some scripting with other platforms. Now after, looking at the documentation, trying the examples and searching through the forum and countless test/try iterations, I really can’t see why I can’t find my mistake. It’s like something is missing in the basic-set up.

So here a simple question:
It is so simple I’m a bit embarassed to even ask: I want to use a variable in a script, but whatever a try, I get errors when saving the script.

alias: Test_input_script
variables:
  target_entity: light.hallway
sequence:
  - service: light.turn_on 
    target: 
      entity_id: "{{ target_entity }}"
mode: single

I get this error:

Message malformed: not a valid value for dictionary value @ data[‘sequence’][0][‘target’][‘entity_id’]

From this, I see that the variable is not evaluated. But why?

I have even tried the example script form the documentation, but get the same error? Is there something I have to set-up to be able to work with fields and variables?

Try this version:

alias: Test_input_script
variables:
  target_entity: light.hallway
sequence:
  - service: light.turn_on 
    data: 
      entity_id: "{{ target_entity }}"
mode: single

Wow. Thanks that works, thanks!

I now understand that the “data” statements makes sure that the variable is evaluated, correct?

Trying to learn from this; then I don’t understand this example is in documentation , as it has the same construct that did not work (in my setup):

      - alias: "Living room lights on"
        service: light.turn_on
        target:
          entity_id: "{{ turn_on_entity }

Full example from the documentation

script:
  wakeup:
    alias: "Wake Up"
    icon: "mdi:party-popper"
    description: "Turns on the bedroom lights and then the living room lights after a delay"
    variables:
      turn_on_entity: group.living_room
    fields:
      minutes:
        description: "The amount of time to wait before turning on the living room lights"
        example: 1
    # If called again while still running (probably in delay step), start over.
    mode: restart
    sequence:
      # This is Home Assistant Script Syntax
      - event: LOGBOOK_ENTRY
        event_data:
          name: Paulus
          message: is waking up
          entity_id: device_tracker.paulus
          domain: light
      - alias: "Bedroom lights on"
        service: light.turn_on
        target:
          entity_id: group.bedroom
        data:
          brightness: 100
      - delay:
          # supports seconds, milliseconds, minutes, hours
          minutes: "{{ minutes }}"
      - alias: "Living room lights on"
        service: light.turn_on
        target:
          entity_id: "{{ turn_on_entity }}"

Yes, in this particular case if you did this:

  - service: light.turn_on 
    entity_id: "{{ target_entity }}"

It would assume the text assigned to entity_id is the name of an entity. It definitely isn’t so it would cause an error.

The addition of target in automations is very new; the documentation (containing examples with target) was updated only about 10 days ago. I don’t think all the kinks have been ironed out yet …

When did that get added? I’ve don’t remember seeing any references to that in any release notes.

And apparently it doesn’t work? :man_shrugging:

About 9 days ago this PR to the Documentation repo updated various automation examples to include the use of target.

However, the following is the PR that introduced a description of target and how it’s used in service calls:

It’s dated 2020/12/13 and represents a significant enhancement to service calls. However, I don’t recall it being mentioned in either the January or February release notes. :man_shrugging:

It’s in the official documentation:

Service calls - Targeting areas and devices

I haven’t experimented with target but, based on Bikey’s experiences, it appears it may not be working as per the documentation’s examples.

1 Like