Return number of entities in group and publish in MQTT

I need to return the number of entities in a group, and publish this message over MQTT.

How would I go about this?

Home Assistant’s Python API can help.

import homeassistant.remote as remote
import paho.mqtt.publish as publish

api = remote.API('127.0.0.1', 'YOUR_PASSWORD')
rules = remote.get_state(api, 'group.all_automations')

entities = len(rules.attributes['entity_id'])

publish.single("entities", entities, hostname="localhost")
1 Like

Hmm, it seems that not all groups are accessible. Of course does the sample work on a fresh installation but self-made groups don’t show up.

Thanks. Where would I put that code, I’m guessing not in the yaml files?