Persistent Entity Augmentation

After working with home assistant for a while I have found myself frustrating over the lack of entity attribute customisation and support for new domains, like sensors and lights. Even using tools like AppDaemon with the set_state function from the hass plugin, the custom changes are overwritten when:

  • The relevant home assistant component or integration updates the entity state.
  • Home assistant restarts

To solve this we need to persist the custom changes and reflect them back on the entities when they are updated. While the optimal solution would be for home assistant to recognize these custom attributes and keep them until removed by users, I wrote an AD app that uses the TinyDB package to simplify interactions with a db.json file, this could easily be ported to a python_script if the file interaction is written out; as the environment doesn’t allow import of packages.

The app listens to two events,

  • homeassistant_start: we reflect all missing attributes onto the entities in home assistant so they will be available to all depending automations etc.
  • state_changed: If the entity is being customized for the first time we store it, else we make sure all attributes with latest data is available in the home assistant entity.

Additionally the state_changed listener also checks for two optional event fields:

  • remove_attributes: a list of attribute keys as strings that will be removed from tracking.
  • siblings: remove the listed attributes from all siblings, eg: if entity is sensor.home_temp, then it removes attributes from all sensors.

These two parameters allow full control over entity representation in home assistant, and the changes are only persistent while the app is running, or database file present. So we can easily revert back to the standard entity representations that are stored in the registry files of home assistant. I also mentioned the creation of new domains, this comes in handy to in cases of composite entities, tying all important data together without creating a ton of sensors and template sensors.

washing_machine = {
    "entity_id": "device.washing_machine",
    "state": "idle",
    "icon": "mdi:washing-machine",
    "detergent_stock": "nominal",
    "fabric_softener_stock": "nominal",
    "last_used_by": "user",
        "schedule": {
        "from": "datetime",
        "to": "datetime",
        "mode": "full",
        "by": "user"

You’ll find the app following the link to my github repo that also contain a few other cool tricks and a deployment scheme with docker compose for a lot of automation services, I will likely post more about them here if there is any interest. If you are interested, keep an eye on it for further releases as I refactor my project to be publicly available.

Github repo: Life automation

1 Like