Notification List of sensors by value

I have a number of apparent temperature sensors set up using GitHub - Limych/ha-apparent-temperature: Sensor of Apparent Temperature for Home Assistant.. Each one represents the conditions inside a greenhouse. I also have an automation setup to send email notifications when one or more of those apparent temperature sensors exceeds certain set points. I have an old-style group setup with these sensors. I am trying to refine the notification to include a list of which of those sensors are above which set points.

I have the following as part of my notification

{% set x = expand('group.greenhousefeelslikegroup') 
      | selectattr('state', 'is_number') 
      | selectattr('state', '>=', '80')
      | selectattr('state', '<', '90')
      | list
    %} {% for item in x %}
      {% print item.name,' - ', item.state|round(1), '°F\n' %} 
      {%endfor%}

That produces the expected list, which might be something like:

Greenhouse 1 - 87.3°F
Greenhouse 3 - 84.6°F

Where I am having trouble getting things to work right is when I try to break out ranges from 90-100°F and above 100°F. I understand that the filtering I’m doing here is trying to compare strings, so it makes sense to me why the comparison breaks down if I plug in 100.

When I try to change my approach to something like:

{{ expand('group.greenhousefeelslikegroup') 
      | selectattr('state', 'is_number') 
      | map(attribute='state') |map('int')
      | select('>=' , '100' | int )
      | list
}} 

I get a list of values like

104
113
101

But through that, I lose the sensor that produced each value. Adding the for loop

I would like to be able to get a notification that looks something like:

The following greenhouses have an apparent temp above 100°F
Greenhouse 2 - 102.5°F
Greenhouse 6 - 107.6°F

The following greenhouses have an apparent temp above 90°F
Greenhouse 1 - 93.7°F
Greenhouse 4 - 98.3°F

The following greenhouses have an apparent temp above 80°F
Greenhouse 3 - 89.7°F
Greenhouse 5 - 84.4°F

Any pointers on how to write the template would be greatly appreciated.

Copy-paste the following template into the Template Editor (Developer Tools → Template) and confirm it reports the desired information.

{% set s = expand('group.greenhousefeelslikegroup') | selectattr('state', 'is_number') | list -%}
{% set n = s | map(attribute='name') | list -%}
{% set v = s | map(attribute='state') | map('int', 0) | list -%}
{% set z = zip(n, v) | list -%}
The following greenhouses have an apparent temp above 100°F
{{ z | selectattr(1, '>=', 100) | map('join', ' - ') | join('°F\n') }}

The following greenhouses have an apparent temp above 90°F
{{ z | selectattr(1, '>=', 90) | selectattr(1, '<', 100) | map('join', ' - ') | join('°F\n') }}

The following greenhouses have an apparent temp above 80°F
{{ z | selectattr(1, '>=', 80) | selectattr(1, '<', 90) | map('join', ' - ') | join('°F\n') }}

If it fails, let me know what it does report (if anything) and I can help you fix it.


Reference

Templating - Iterating Multiple Objects

That produces exactly what I was looking for.

Thanks

1 Like