I just confirmed that the proposed group-based technique works fine on startup.
The following automation creates group.battery_powered
and populates it with all sensors whose names end with “_battery_level”. It does this on startup and whenever Reload Groups is executed (more triggers can be added).
- alias: 'Create Groups'
trigger:
- platform: homeassistant
event: start
- platform: event
event_type: 'call_service'
event_data:
domain: 'group'
service: 'reload'
action:
- service: group.set
data:
object_id: battery_powered
name: 'Battery Powered'
entities: >
{% set ns = namespace(sensors = []) %}
{% for entity in states.sensor if entity.entity_id.endswith('_battery_level') %}
{% set ns.sensors = ns.sensors + [entity.entity_id] %}
{% endfor %}
{{ ns.sensors }}
The Template Binary Sensor now monitors fewer sensors. The for-loop statement is reduced to:
{% for entity in expand('group.battery_powered') %}
Like so:
battery_devices_issues:
friendly_name: 'Battery Devices Issues'
device_class: problem
value_template: >-
{% set status = (state_attr('binary_sensor.battery_devices_issues', 'status')) %}
{{ status.empty|count + status.dead|count > 0 }}
attribute_templates:
status: >-
{% set ns = namespace(empty = [], dead = [], low = [], good = []) %}
{% for entity in expand('group.battery_powered') %}
{% set name = entity.object_id[:-14] %}
{% set level = states(entity.entity_id) %}
{% set changed = states['sensor.'~name~'_temperature'].last_changed %}
{% if changed != null %}
{% if level == 'unavailable' %} {% set ns.empty = ns.empty + [name] %}
{% elif now() - changed >= timedelta(hours=2) %} {% set ns.dead = ns.dead + [name] %}
{% elif level|int < 40 %} {% set ns.low = ns.low + [name] %}
{% else %} {% set ns.good = ns.good + [name] %}
{% endif %}
{% endif %}
{% endfor %}
{{ "{{\"empty\": {}, \"dead\": {}, \"low\": {}, \"good\": {} }}".format(ns.empty, ns.dead, ns.low, ns.good) }}
I joined this community forum in October 2018. At the time I was experimenting with openHAB (for several months). I decided I preferred something that behaved more like Premise so I began exploring Home Assistant. As I slowly became comfortable with it, I took my time to carefully transfer/convert what I had created in Premise to Home Assistant. I waited until Home Assistant’s UPB integration was finished before transferring control of all lighting. I don’t recall when Home Assistant was no longer in “test” but in “production” in my home but it’s between 1 and 2 years now.
In Premise, I developed several integrations notably for the ELK M1 security panel, Environment Canada weather service, HAI Omnistat2 RC-2000 thermostat, etc. Most of my work is posted on the Cocoontech.com site (best home automation community back in the day; now just a shadow of its former self). The last one I created (early 2018) was an MQTT integration to allow Premise to communicate with other systems. That’s when I started dabbling with openHAB followed by Home Assistant in late 2018.
What I haven’t mentioned is that development of Premise was halted in 2006 and the product became freely available to the public (original price was over US$1000). I jumped on board in 2007. It’s a testament to its advanced architecture that users were able to continue enhancing it even though it remained closed-source.
Anyway, all ancient history now.