No, it’s backwards. This is not the way to build this. You don’t control backend things in that way from a frontend. Nobody that understands software engineering would do this.
It’s a bit involved, but here’s my automation for this kind of thing:
- alias: "Check For Low Batteries"
initial_state: true
variables:
level: 5
# https://community.home-assistant.io/t/variable-in-automation-not-working-as-expected/431591/2
monitored_batteries: >-
{{
states.sensor
| selectattr('attributes.monitor', 'defined')
| selectattr('attributes.monitor', 'eq', True)
| rejectattr('state', 'in', ['unavailable', 'unknown', 'none'])
| selectattr('attributes.device_class', 'eq', 'battery')
| map(attribute='entity_id')
| list
}}
trigger:
platform: time
at: "09:00:00"
condition: >-
{{
expand(monitored_batteries)
| map(attribute='state')
| map('int')
| select('lt', level)
| list
| count
> 0
}}
action:
- service: notify.mobile_app_ceres
data:
title: "Batteries"
# https://community.home-assistant.io/t/recommended-ways-to-manage-devices-and-entities-names/243815/12
message: >
The following devices have less than {{ level }}% charge:
{%- for b in monitored_batteries %}
{%- if states(b) | int < level and not is_state(b, 'unavailable') %}
- {{ state_attr(b, 'friendly_name') | replace(' Battery', '') }}: {{ states(b) | int }}%
{%- endif -%}
{%- endfor %}
data:
group: "batteries"
url: homeassistant://navigate/lovelace/devices
All I have to do now is to add a special attribute to a device with a battery I want monitored by this, for example:
sensor.pieter_bedside_light_button_battery:
friendly_name: Pieter's Bedside Light Button Battery
monitor: True
It’s even easier if you want to monitor all batteries. In my case, I only want to monitor certain batteries. In other words, this:
{{
states.sensor
| selectattr('attributes.monitor', 'defined')
| selectattr('attributes.monitor', 'eq', True)
| rejectattr('state', 'in', ['unavailable', 'unknown', 'none'])
| selectattr('attributes.device_class', 'eq', 'battery')
| map(attribute='entity_id')
| list
}}
can then be simplified to this:
{{
states.sensor
| rejectattr('state', 'in', ['unavailable', 'unknown', 'none'])
| selectattr('attributes.device_class', 'eq', 'battery')
| map(attribute='entity_id')
| list
}}