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:
A sensor group (“vm_starting_sensor”) of type “maximum,” whose state is the max value of any of the 25 sensors within the group.
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)
A script set up with a Field to collect the aforementioned max entity ID.
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”).
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.
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!
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.)
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:
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.
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:
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 %