Get the element which has the max state value

Can anyone help me out filtering the max value variable (entity id and its state value) by using filters only? I need a single liner solution. Thanks. Below is an example:

well this is list

[
<template TemplateState(<state input_number.spotify_playback_volume_bathroom_speaker=5.0; initial=None, editable=True, min=0.0, max=100.0, step=5.0, mode=slider, unit_of_measurement=%, friendly_name=Spotify Playback Volume - Bathroom Speaker @ 2023-11-29T10:56:45.390229+01:00>)>,

<template TemplateState(<state input_number.spotify_playback_volume_bedroom_clock=10.0; initial=None, editable=True, min=0.0, max=100.0, step=5.0, mode=slider, unit_of_measurement=%, friendly_name=Spotify Playback Volume - Bedroom Clock @ 2023-11-29T11:16:39.865984+01:00>)>,

<template TemplateState(<state input_number.spotify_playback_volume_living_room_clock=20.0; initial=None, editable=True, min=0.0, max=100.0, step=5.0, mode=slider, unit_of_measurement=%, friendly_name=Spotify Playback Volume - Living Room Clock @ 2023-11-29T11:16:43.477724+01:00>)>,

<template TemplateState(<state input_number.spotify_playback_volume_kitchen_clock=15.0; initial=None, editable=True, min=0.0, max=100.0, step=5.0, mode=slider, unit_of_measurement=%, friendly_name=Spotify Playback Volume - Kitchen Clock @ 2023-11-29T11:16:41.696763+01:00>)>,

<template TemplateState(<state input_number.spotify_playback_volume_office_room_clock=25.0; initial=None, editable=True, min=0.0, max=100.0, step=5.0, mode=slider, unit_of_measurement=%, friendly_name=Spotify Playback Volume - Office Room Clock @ 2023-11-29T11:16:46.284887+01:00>)>
]

here you can see that the list is filled with different TemplateStates. Now I wish to filter out only the TemplateState which has the maximum value in its state.

{{ states.input_number | selectattr('entity_id','search', 'spotify_playback_volume_') | selectattr('state', '>', '0.00') | max(attribute='state') }}

having this filter gives out the following output:

<template TemplateState(<state input_number.spotify_playback_volume_bathroom_speaker=5.0; initial=None, editable=True, min=0.0, max=100.0, step=5.0, mode=slider, unit_of_measurement=%, friendly_name=Spotify Playback Volume - Bathroom Speaker @ 2023-11-29T10:56:45.390229+01:00>)>

which is wrong as you can see the max state value is not in the input_number.spotify_playback_volume_bathroom_speaker but input_number.spotify_playback_volume_office_room_clock

I’ve responded here: Templating custom filters - #47 by Troon

1 Like