How to create custom filter, or list from for loop?

I’m trying to get the minimum value from a text-derived numeric value (e.g. 99d20h33m) in sensors from a group (fan.whole_house_ventilation).

I can get the values individually:


{% macro dhm_val(str) %}
{{str | regex_findall_index(find='(.*)d', index=0, ignorecase=False)|float}}
{% endmacro %}

{% for entity in expand('fan.whole_house_ventilation') %}
  {{dhm_val(states('sensor.'+entity.entity_id | regex_findall_index(find='fan\.(.*)',index=0,ignorecase=False)+'_filter_change_in'))}}
{% endfor %}

But this just prints the individual values, it doesn’t return a list.
How would one go into getting the min of all the individual results?

I wish I could write this as a filter, e…g

{% macro get_dhm_val(entity) %}
  {{ dhm_val(states('sensor.'+entity.entity_id | regex_findall_index(find='fan\.(.*)',index=0,ignorecase=False)+'_filter_change_in')) }}
{% endmacro %}  

{{ expand('fan.whole_house_ventilation') | get_dhm_val | min }}

But that doesn’t work, and it seems creating filters is much more burdensome (happy to be proved wrong!)

i don’t quite grock how you want to parse the text value. looks like your code is only looking at the date part? but setting aside how you extract the value from the text, which i think you have already figured out, your main question is how to get the min of the reslts.

take a look at this and see if this helps:

{% set entities = [ 'sensor.sensor1', 'sensor.sensor2', 'sensor.sensor3' ] %}
{% set ns = namespace(items=[]) %}

{% for entity in entities %}
    {% set ns.items = ns.items + [ (entity, states(entity)) ] %} 
{% endfor %}

{{ ns.items | sort(attribute=1) | map(attribute=0) | first}}

this creates a list with all of the entities and the values i want to sort on. i’ve used ‘states(entity)’ but you can replace it with dhm_val.

then at the end, i do a sort using attribute=1 (which will sort on your dhm_val). then i map(attribute=0) which just picks the entity id, and ask for the first. since it’s sorted lowest to highest, the first will be the lowest.

What about something like:

{% set time_list =  
state_attr('fan.whole_house_ventilation', 'entity_id') | map('replace', 'fan.', 'sensor.')
| map('regex_replace', '$', '_filter_change_in') | map('states') | list %}
{% set ns = namespace(td=[]) %}
{% for time in time_list %}
  {% set (d, h, m) = (time|regex_replace('[hmd]', ',')).split(',')|reject('eq', '')|map('int')|list %}
  {% set ns.td = ns.td + [timedelta(days=d, hours=h, minutes=m)] %}
{% endfor %}
{{ns.td | min}}

Thank you all. I realised I was missing a “| list”

I ended up doing this, which works:

{% macro dhm_val(str) %}
  {{str | regex_findall_index(find='(.*)d', index=0, ignorecase=False)|float}}
{% endmacro %}

{% macro get_dhm_val(entity) %}
  {{ dhm_val(states('sensor.'+ entity.entity_id | regex_findall_index(find='fan\.(.*)',index=0,ignorecase=False)+'_filter_change_in')) }}
{% endmacro %}  

{% set fans_day_to_filter_replace = namespace( members = [] ) %}
{% for entity in expand('fan.whole_house_ventilation') %}
  {% set fans_day_to_filter_replace.members = fans_day_to_filter_replace.members + [get_dhm_val(entity)|int] %}
{% endfor %} {{ fans_day_to_filter_replace.members | min}}