Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Retrieving all entities of a specific device_class #356

Unanswered
selmaohneh asked this question in Q&A
Discussion options

Hi!

I am finally planning on pushing a notification when any of my battery-powered devices is getting low. For that to work I need to get all entities that have the device_class battery (or at least have battery in the entitiy_id).

Has anyone already done that and can share some scripts?

What would be the correct approach? Is it iterating over all results from state.names(...) ?

Thank in advance for any hints!

You must be logged in to vote

Replies: 2 comments

Comment options

Repinging this thread from almost 3 years ago, I couldn't find an answer anywhere but I'd love to be able to do this as well in pyscript. E.g. I have a bunch of binary sensors for my door and windows. The binary_sensors are from KNX and in KNX they get the device_class door:

- name: Livingroom
 state_address: 2/1/1
 device_class: door
 sync_state: true

Now the following YAML code works well to give my a markdown list of strings for all open doors (state == 'on', device_class=='door):

{%- for x in states.binary_sensor | selectattr('attributes.device_class', 'defined') | selectattr('state','eq','on') %}
 {%- if "door" in x.attributes.device_class %}
## <ha-icon icon="mdi:door-open"></ha-icon> {{ x.name | trim }} is open
 {%- endif -%}
{%- endfor -%}

in pyscript this then becomes:

def doors_open_in_markdown():
 doors_open=""
 for x in state.names("binary_sensor"):
 if state.get(x) == 'on':
 if len(doors_open) > 0: doors_open +="\n"
 try: friendly_name = state.getattr(x)['friendly_name'].strip()
 except KeyError: pass
 doors_open=doors_open+f"### <ha-icon icon='mdi:door-open'></ha-icon> {friendly_name} is open"
 return doors_open

But that gives me all binary_sensors in the house that are 'on', not just the door sensors because I can't filter for device_class, any suggestions other than playing with filtering based on entity_ids or the actual names that then need to match some specific pattern instead of filtering for the right device_class? I would hate to do that since a) the right information is of course there somewhere and b) it is error prone if some name happens to match the filters... Thanks!

You must be logged in to vote
0 replies
Comment options

I get all entities with state.names() filter them out, and join them in a group.batteries with group.set(object_id=group_entity_id, entities=filtered_entities)
my regex filters are:

- "^.*(nominal|voltage|chemistry|setpoint|date|sensitivity|runtime|count|health|state|temp|temperature|power|battery_power|power_meter|replaced|battery_type|last_replaced|total_out_power|total_in_power|active_power|group|battery_last_replaced).*$"
- "^.*(battery_power|power_meter|total_out_power|total_in_power|active_power|_battery_last_replaced|_battery_type)$"
- "sensor\\..*last_replaced.*"
- "sensor\\..*_battery_plus"

then there's a cron function

BATTERY_CHARGE_TRIGGER_LEVEL = 30
@time_trigger('once(10:00:00)')
@state_active("input_boolean.battery_low_notification == 'on'")
def func_battery():
 msg = '🪫'
 send = False
 group_entity = entity('group.batteries_group')
 battery_entity_ids = group_entity.attrs().get('entity_id', [])
 for entity_id in battery_entity_ids:
 entity_ = entity(entity_id)
 entity_state = entity_.state()
 if not str(entity_state).isnumeric():
 continue
 entity_state = int(entity_state)
 if entity_state < BATTERY_CHARGE_TRIGGER_LEVEL:
 entity_fn = entity_.friendly_name()
 msg += f"\n{entity_fn}\n{entity_id}\n{entity_state}"
 send = True
 if send:
 tools.telegram_message(msg, disable_notification=True)

the function cannot be used as is, but you get the idea.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /