Using fields in a script

Can I use fields to pass entity_id to script and then perform necessary actions with real equipment?

For example, I would like to pass into the script entity_id for power on and entity_id for light on and then in a loop to make the light on, etc., I have such a script, but since there are many lights I would like to create a script with fields and without changing the script itself to pass it different entity_id. If for my task it is necessary something else, not fields, also give me a hint.

Hello D,

Yes, that is exactly what fields are for.

1 Like

can you show me a example ? What type of fields do I need ?

I use them in many of my script blueprints and in places in my config.
Blueprints & HA Config

1 Like

you have a lot of files)) could you give the name of the yaml file where the script gives some entity_id as input and then further with it is made some actions ?

Script fields are described in the following documentation:

It also contains examples and a video tutorial.

2 Likes

I didn’t find an example there where an entity_id is passed to a script field and then this field is used to interact with the equipment

sequence:
  - action: light.turn_on
    metadata: {}
    data: {}
fields:
  svet_pitanie:
    selector:
      text: null
    name: svet_pitanie
    required: true

Here I have created a field named svet_pitanie, now how to use it in action to turn on the light ?

fields:
  svet_pitanie:
    selector:
      text:
    required: true
sequence:
  - action: light.turn_on
    target:
      entity_id: "{{ svet_pitanie }}"
1 Like

its work thanks!
but how I can made loop If my field svet_pitanie is multiply

tryed this but got an error

repeat:
  sequence:
    - action: light.turn_on
      target:
        entity_id: "{{ repeat.item }}"
  for_each: "{{ svet_pitanie }}"

Your text selector results in a string. You cannot do a repeat over a string, without explicitly splitting it up into a list.

Also there would be no point in the repeat either way. A light.turn_on action can operate on a list of light entities.

Much better design would be to not use a text selector, but either an entity or a target selector, set to filter on only light entities.

Read the documentation.

1 Like