When you can mash / plead / beat the templating engine into doing what you want in #HomeAssistant it is incredibly powerful.
Good freaking grief it took far too much effort to make an average of a dynamic set of sensors (Air Quality sensors inside my house).
=> More informations about this toot | More toots from warthog9@social.afront.org
@warthog9 i have not managed to do this myself yet.
=> More informations about this toot | More toots from pearofdoom@cloudisland.nz
@pearofdoom when I've got some time tomorrow I'll copy/paste the package I wrote and post it here I guess, should be trivially adjustable to other needs
=> More informations about this toot | More toots from warthog9@social.afront.org
@warthog9 I recently struggled with this as well, trying to create a sensor that was an average of all temperature sensors. Here's what I came up with: https://gist.github.com/tkroo/dcf9822052fd4900c14fae4712e382f4
=> More informations about this toot | More toots from tkroo@fosstodon.org
@tkroo I'll check in the morning, I'm curious to see if there's a better way than what I did.
The right solution would be for min_max to support templating in the entity_id's but no such luck yet
=> More informations about this toot | More toots from warthog9@social.afront.org
@tkroo Interesting, you might have a couple of things I can steal to simplify mine but let me paste what I've got here, in all it's extra debug glory
customize:
sensor.aqi_pm25_indoor:
aqi_sum:
aqi_count:
template:
- name: "Indoor AQI (pm2.5)"
unique_id: sensor.aqi_pm25_indoor
state: >
{% set ignored_aqi_entities = state_attr('group.ignored_aqi_entities', 'entity_id') %}
{% set entities_aqi_list = states
| rejectattr('state','in',['unknown','unavailable']) | rejectattr('entity_id','search','device_tracker')
| rejectattr('entity_id','search','wimp')
| rejectattr('entity_id','search','sensor.aqi.pm25.indoor')
| rejectattr('entity_id','search','sensor.aqi_pm25_indoor')
| rejectattr('entity_id','search','rx')
| rejectattr('entity_id','search','tx')
| selectattr('entity_id', 'search', 'aqi')
%}
{% set entities_aqi_list = entities_aqi_list
| rejectattr('entity_id', 'in', ignored_aqi_entities)
if ignored != none
else entities_aqi_list %}
{% set ns = namespace( aqi_count = 0, aqi_total = 0 ) %}
{% for entity in entities_aqi_list | map(attribute='entity_id') %}
{% set ns.aqi_total = ( states(entity) | float ) + ns.aqi_total %}
{% set ns.aqi_count = ns.aqi_count + 1 %}
{% endfor %}
{{ ( ns.aqi_total / ns.aqi_count ) | round(0) }}
attributes:
unit_of_measurement: AQI
device_class: aqi
[#] OPTIONAL - Add entities you want to ignore to this group. Delete if not using group.
group:
ignored_aqi_entities:
entities:
- sensor.calculated_aqi_5 # outside sensor
- sensor.indoor_aqi_pm2_5
=> More informations about this toot | More toots from warthog9@social.afront.org
@warthog9 Thanks for sharing. I like the how you set up an ignore group. I'll be stealing that :) Also, I see that your reject filters are before selects and that made me wonder if the order is important when the yaml/jinja is interpreted. It seems likely more efficient to put reject conditions at the start. So I think I'll adopt that as well. Cheers.
=> More informations about this toot | More toots from tkroo@fosstodon.org
@warthog9 The big problem is the dynamic part, preventing the use of a sensor group?
=> More informations about this toot | More toots from AngryAnt@mastodon.gamedev.place
@AngryAnt
Agreed. It's easy using a helper to take an average of a fixed set of sensors. Or as I did to take the median of temperature sensors in my two HVAC zones. But if you have a dynamic set of sensors.... That's seems both weird, and hard.
@warthog9
=> More informations about this toot | More toots from dlakelan@mastodon.sdf.org
@dlakelan @AngryAnt Dynamic list of sensors isn't even that weird as sensors may be up / down / out of battery / etc so you only want to sum up what's online / active / etc.
The only reason it's hard per-se is just figuring out how to get the template(s) to work the way HASS has thought they should work and working back from there. Obnoxiously the only way I figured out how to handle the averaging was a bit more rudimentary than I wanted but it works
=> More informations about this toot | More toots from warthog9@social.afront.org
@warthog9 @AngryAnt
Hmm... does the homeassistant helper not ignore sensors that are offline? What does it do, take the last known value?
My experience is that taking the median of multiple temperature sensors is working well for me whether or not sensors are offline. But I haven't actually tested off/online
=> More informations about this toot | More toots from dlakelan@mastodon.sdf.org
@dlakelan @AngryAnt it might, but it's also a lot easier to say "all entities that have aqi or similar in their entity_id except these or another constraint" also means I never have to go manuall add something to get it to work, and the list could be 10s to 100s of devices so manual is not ideal.
=> More informations about this toot | More toots from warthog9@social.afront.org This content has been proxied by September (ba2dc).Proxy Information
text/gemini