How to reference an entity ID from a sensor group in a script?

Hi! I’m trying to write a script that does some logical evaluation of a set of 25 sensors. When triggered, the script needs to reference attributes of the specific sensor with the highest value.

Currently I have the following up and running:

  1. A sensor group (“vm_starting_sensor”) of type “maximum,” whose state is the max value of any of the 25 sensors within the group.
  2. An automation which, when triggered, turns on a script and feeds it the attribute “Max entity ID” (I think I remember seeing this represented as “max_entity_id” somewhere, but I cannot find confirmation of that anywhere in the documentation right now)
  3. A script set up with a Field to collect the aforementioned max entity ID.

Automation action:

Script field:

Now here is where I’m stuck: I want my script to take that max entity ID and use it to evaluate an attribute (“row”) of that specific sensor entity (the one that has the highest value).

I’ve tried doing this with a Conditional block, but it doesn’t seem to like my notation (“e is undefined”).

Script action:

Turning the entity ID into a variable from the get-go (and then using it later) would actually be ideal. But I’m really struggling to understand templating, or to find any documentation or examples of how the Define Variables action is supposed to work in scripting.

Script action:

If you have any experience with any of these problems, I’d really appreciate some help. I’m trying to do as much as possible within the GUI, but if I need to add code then so be it!

Thanks in advance.

So something like this?

{{ state_attr(max_entity_id,'row') }}

It might help if you share the automation and script YAML rather than screen shots.

It doesn’t like it.

Message malformed: Entity {{ state_attr(max_entity_id is neither a valid entity ID nor a valid UUID for dictionary value @ data[‘sequence’][3][‘if’][0][‘entity_id’]

I tried the friendly name too (“Max entity ID”) with both single and double quotes, and got the same error.

Yes, for sure. You’ll notice these are numbers rather than sensors because I’m prototyping right now and the sensors themselves haven’t arrived… I don’t think that changes the outcome though. (I’ve checked with a badge on my dashboard and sensor group correctly reports both the max value of the entity group and the entity ID of the number with the highest value.)

Here is the automation:

alias: Start Village Map Sequence
description: ""
triggers:
  - trigger: numeric_state
    entity_id:
      - input_number.a1
      - input_number.a2
      - input_number.a3
      - input_number.a4
      - input_number.a5
      - input_number.b1
      - input_number.b2
      - input_number.b3
      - input_number.b4
      - input_number.b5
      - input_number.c1
      - input_number.c2
      - input_number.c3
      - input_number.c4
      - input_number.c5
      - input_number.d1
      - input_number.d2
      - input_number.d3
      - input_number.d4
      - input_number.d5
      - input_number.e1
      - input_number.e2
      - input_number.e3
      - input_number.e4
      - input_number.e5
    for:
      hours: 0
      minutes: 0
      seconds: 1
    above: 5
conditions:
  - condition: state
    entity_id: input_boolean.vm_enabled
    state: "on"
  - condition: not
    conditions:
      - condition: state
        entity_id: input_boolean.vm_complete
        state: "on"
      - condition: state
        entity_id: input_boolean.village_map_fail
        state: "on"
      - condition: state
        entity_id: input_boolean.village_map_in_progress
        state: "on"
actions:
  - action: input_boolean.turn_on
    metadata: {}
    data: {}
    target:
      entity_id: input_boolean.village_map_in_progress
  - action: script.village_map
    metadata: {}
    data:
      vm_starting_sensor: max_entity_id
mode: single

And here is the script. (Unfinished since I’m stuck on this step of referencing the entity.)

sequence:
  - action: timer.cancel
    metadata: {}
    data: {}
    target:
      entity_id: timer.vm_timeout
  - action: timer.start
    metadata: {}
    data: {}
    target:
      entity_id: timer.vm_timeout
  - wait_for_trigger:
      - trigger: state
        entity_id:
          - sensor.vm_sensors
        attribute: max_entity_id
    continue_on_timeout: false
  - if:
      - condition: numeric_state
        entity_id: vm_starting_sensor.max_entity_id
        below: 3
        attribute: row
    then:
      - action: input_boolean.turn_on
        metadata: {}
        data: {}
        target:
          entity_id: input_boolean.vm_complete
    else:
      - action: input_boolean.turn_on
        metadata: {}
        data: {}
        target:
          entity_id: input_boolean.village_map_fail
alias: Village Map
description: ""
fields:
  vm_starting_sensor:
    selector:
      attribute:
        entity_id: sensor.vm_sensors
    name: VM Starting Sensor
    default: max_entity_id
    required: true

Thanks again.

Where is the variable max_entity_id defined in your automation?

The variable vm_starting_sensor contains null as you have not defined the variable max_entity_id anywhere in your automation. You probably want this instead:

  - action: script.village_map
    metadata: {}
    data:
      vm_starting_sensor: "{{ state_attr('sensor.vm_sensors','max_entity_id') }}"

In your script you have this:

What exactly are you waiting for?

This waits for any change of the attribute max_entity_id, so the entity id you passed to the script will be out of date after this triggers.

Once you have sorted that this:

  - if:
      - condition: numeric_state
        entity_id: vm_starting_sensor.max_entity_id
        below: 3
        attribute: row

Needs to be:

  - if:
      - condition: numeric_state
        entity_id: "{{ vm_starting_sensor }}"
        below: 3
        attribute: row

As I really don’t have a clue what you are attempting here I really can’t help much beyond those syntax issues.

Unless I’m completely misunderstanding the relationship between the GUI and the YAML, I believe the variable you’ve identified is the YAML version of what’s shown here as “Max entity ID,” which is an attribute of “VM Starting Sensor” (my 25-entity Sensor Group). I selected it from a dropdown. Can you help me understand why that variable is undefined or not being passed correctly to the script? It seems like a very real attribute of this sensor group.

Edit: I think I understand your point about the YAML. My understanding was that max_entity_id is an attribute of the sensor group vm_sensors. Do you know how I can properly define this argument in the GUI so that I can correctly pass it to the script? I thought I was following this guide in the docs (“Passing Variables to Scripts”): add a field to the script, name the field, and it will show up in other UI editors (such as automations that call the script). This all seemed to work as expected, as you can see that “VM Starting Sensor” showed up as a field in the automation once I had added it in the script.

What is the secret missing step here? It looks to me like the docs are saying (and the GUI options are reinforcing) I can use the “max_entity_id” field in my automation when I call the script I created it in, but as you point out, “max_entity_id” is not defined anywhere else in the automation’s YAML. It does appear to be a selectable property of vm_starting_sensor, though.

This felt overly complicated for the scope of the question but basically what I am doing here to start is looking for two separate sensor changes within 10 seconds to confirm activity. (I am considering an individual sensor change within 10 seconds to be a fluke and discarding it. There is a 10-second timer called “VM Timeout” that you’ll see referenced here, which triggers a separate automation that stops this script.) Once two sensors have changed within 10 seconds, I want to know the attributes of the highest-value sensor. Does that agree with what you’re seeing in the YAML?

Later when I build out the logic here a bit more, I will be wanting to reference all of the Max Entity IDs that occur within 10 seconds of each other, including the first incoming one. This would really be a lot to explain in detail but essentially I am trying to capture a sequence or set of sensors and act on them both individually as they occur and as a group after a timeout. Kind of like a game of Battleship – for example, if A2, A3, A4, and A5 all read High within a certain amount of time, the ship is sunk.

Referencing the value of other entities like this is advanced and requires the use of YAML and Jinja.

Thanks for your help. You were right, some of it had to be done with templates & Jinja2 syntax.

I was ultimately able to save and later reference the sensor I wanted by defining a variable at the start of the script instead of trying to pass a value from the automation. The code that I ended up writing was:

  - variables:
      vm_starting_sensor: "{{state_attr('sensor.vm_sensors', 'max_entity_id')}}"

and later, when evaluating:

  - if:
      - condition: template
        value_template: "{{float(states(vm_starting_sensor)) > 8 }}"
(...etc)

After a drawn-out struggle, my observation is that 90% of the difficulty of learning the Jinja syntax / using it in HA seems to be concentrated on understanding when to use the five characters ", ', {, }, and % :roll_eyes:

Best,
Ethan

There are a few Cookbook articles here that may help: The Home Assistant Cookbook - Index

In particular: What's with all the curly brackets? Jinja template delimiters and whitespace control

1 Like