Testing state of entities passed to a script within it

Hi,
I would like to implement a script that would be called it in my automations for any of my media player entities and:

  1. Pass each time affected entity to my script - what was easy, so I have already manged it.
  2. In the script I would like to test the state of the passed entity and act corresponding to the result. I have found no way yet how to test the sate of an entity I passed to my script, in the script. There is a field selection “state”, but it needs necessarily a concrete entity ID and I do not know how to deal with this challenge.

I hope someone can help me with a solution for my point 2.

This reads like you are creating the script via the UI. Might be easier with YAML, or at least in YAML mode in the UI. I suspect you’ll end up with some template fields.

Give an example of what you’ve tried so far with the passed-in entity (presumably an ID?), pasted as properly-formatted YAML that should look like this:

alias: Holiday counter
sequence:
  - service: input_number.set_value
    target:
      entity_id: input_number.days_off
...

Clearly explain what you are trying to achieve.

Hi Troon!
Thanks a lot for you reply. What I am trying to achieve shows the following simplified example - not working: “if condition”

alias: stopEntity
sequence:
  - if:
      - condition: state
        entity_id: "{{ entity1 }}"   
        state: "on"
    then:
      - service: media_player.turn_off
        target:
          entity_id: "{{ entity1 }}"
fields:
  entity1:
    selector:
      entity: {}
    name: Entity1
    required: true
    description: First passed entity
description: "Example stopEntity"

The state condition docs do not suggest that entity_id supports templates. Try this instead, using a template shorthand condition:

alias: stopEntity
sequence:
  - if:
      - "{{ is_state(entity1, 'on') }}"
    then:
      - service: media_player.turn_off
        target:
          entity_id: "{{ entity1 }}"

Thanks a lot. It works!

1 Like

Good. I dread to think what would happen if you turned it off for a second time… :grin:

We are now watching Wimbledon (tennis), so I can test repeated calls of my example script later. Do you mean a second call immediately after the first one or after a while?

BTW: The “Fields” declaration provides a flag called “multiple”, I assume with this flag it would be possible to implement a script, lets call it “stopEntities”, in order to manage multiple entities in a loop, e.g.: “while”, “until”, entity[index] != “null” … or something similar.
In this context I tried by myself to find answers to some questions , but was not really successful. Would you have any tips/suggestion/ideas in questions:
1, If the multiple variables (passed to a script) are provided in “script” in form of an indexed table? E.g. entity[max_index+1] while index range is from 0 to max_index - is the range actually correct?
2. If “script” provides an internal variable/parameter like number of passed variables? I mean one that could be used e.g. in a repeat/while loop. Introducing of an additional “fields” variable, lets call it “numberOfEntifies”, would be an alternative.
3. Or if the condition “entity[any index] != null” would be a proper criteria for reaching the last variable, passed to “script”.

Edited (working example for multiple entities):

alias: stopEntities
sequence:
  - repeat:
      count: "{{ number_of_entities }}"
      sequence:
        - if:
            - condition: template
              value_template: "{{ is_state(entity[repeat.index-1], 'on') }}"
          then:
            - service: media_player.turn_off
              target:
                entity_id: "{{ entity[repeat.index-1] }}"
              enabled: true
        - service: notify.loginfo
          data_template:
            message: Current Index
fields:
  entity:
    selector:
      entity:
        multiple: true
    name: Entity
    required: true
    description: passed entity list
    default:
      - media_player.enigma2_media_player
      - media_player.fire_tv_firetv_dom
  number_of_entities:
    selector:
      number:
        min: 1
        max: 6
        step: 1
    name: number of entities
    default: 2
    required: true
description: Example stopEntities
icon: mdi:script