I’ve recently deployed the Battery Notes integration and it has added a Battery Replaced sensor (& a Button Press sensor which updates the _battery_replaced sensor with current time) to devices that I choose in the UI. I managed to come up with the Jinja below which returns the matching button sensor (or nothing) and I think I figured out how to put that into my automation which triggers when battery sensor state change increases bt +10 or to 100 so that it presses the button if the sensor has the matching button sensor.
IF button.xyz exists
- service: button.press
entity_id: button.xyz
The Jinja test:
{% set entity_id = "sensor.bathroom_battery" %} <== This will be the trigger.entity_id
{%- set device_id = device_id(entity_id) %} <== and use trigger.entity_id here
{% if (device_id != None) %}
{% for sensor in (device_entities(device_id) | select('search','button.*battery_replaced')) -%}
{{ sensor }}
{%- endfor %}
{% endif %}
prints:
and my new IF-THEN in the automation:
if:
- condition: template
value_template: >
{%- set device_id = device_id(trigger.entity_id) -%}
{%- if (device_id != None) -%}
{%- for sensor in (device_entities(device_id) |
select('search','button.*battery_replaced')) -%}
{{ has_value(sensor) }}{%- endfor -%}{% endif -%}
then:
- service: button.press
metadata: {}
data: {}
target:
entity_id: >-
{%- set device_id = device_id(trigger.entity_id) -%}{%- if (device_id !=
None) -%} {%- for sensor in (device_entities(device_id) |
select('search','button.*battery_replaced')) -%} {{ sensor }}{%- endfor
-%}{% endif -%}
Crossing my fingers!