Recorder exclude by platform?

I’m in the process of setting up room presence detection using the mqtt_room component. I’m not interested in keeping any of the database records for these items, as long as I know the most recent state.

Is there a way to exclude all sensors for the platform mqtt_room? I can see how to exclude a domain, or an entity, but ideally, I’d like to just categorically exclude all mqtt_room sensors, but include all my other sensors.

Here’s what I’m currently working with, but I’m planning on adding several new items to room tracking in the near future, and I’d like to avoid adding maintenance overhead.

recorder:
  purge_interval: 14
  purge_keep_days: 10
  exclude:
    domains:
      - scene
      - automation
      - weblink
      - updater
    entities:
      - sun.sun
      - sensor.yr_humidity
      - sensor.yr_precipitation
      - sensor.yr_wind_speed
      - sensor.ibeacon_bt_room
      - sensor.mi_band_bt_room
      - sensor.mi_flora_a_bt_room
      - sensor.mi_flora_b_bt_room
      - sensor.puck_bt_room

there may be a way to do it with glob. I’m not sure. @anon43302295 thoughts?

Can’t currently be done, unfortunately.

The ‘shortcut’ is to put a for loop in to the developer tools jinja template and get the list of mqtt_room sensors and then copy them over manually.

Should save a bit of time, but won’t be dynamic as new devices are added.

Keep a copy of the for loop commented out in your configuration file and reuse it whenever you need.

I feel like I’m being a dunce here, but I can’t see a way to get a sensor’s platform on the state object. Here’s what I would have hoped would work, but doesn’t:

{% for state in states.sensor -%}
  {% if state.platform == 'mqtt_room' %}
    - {{ state.entity_id }}
  {% endif %}
{%- endfor %}

Which isn’t surprising since https://www.home-assistant.io/docs/configuration/state_object/ doesn’t talk about the platform at all. Am I missing something painfully obvious here?

Are you trying to do this in the template editor? If so, you don’t have access to the states object’s platform. The only attributes that exist on a state object in the template editor are documented here:

You’d have to use a unique identifier in the object_id.

{% for state in states.sensor -%}
  {% if 'mqtt' in state.object_id %}
    - {{ state.entity_id }}
  {% endif %}
{%- endfor %}

Assuming that your object_id’s contain the characters ‘mqtt’

1 Like

Thanks @petro - that’s the same page I linked to above. I was attempting to do what @anon43302295 suggested above, but since that isn’t possible it’s going to be easier to manually grep through my configuration to grab the names of the entities with a given platform than it is to rename them all to a given naming convention and update any and all references to them within my automations etc.

Thanks for the response - it’s appreciated.

Sorry to bump this old topic but I have to say: big thanks for this! Otherwise I would have writen 116 dark_sky sensors manually.

1 Like

oh hell no. Be lazy my friend.

2 Likes

This is an old topic but still actual.
I really like the idea to be able to exclude/include entities to the recorder by the platform. The problem is that platform isn’t available in state.

Is there any other way to get platform from entity_id?
Looking at recorder code I noticed that exclude/include is controlled via filter https://github.com/home-assistant/core/blob/e13f78dfc5f79c23759a070de76f4aff8388845a/homeassistant/helpers/entityfilter.py#L44
so if I can get the platform from entity_id I can potentially add two more options to that generate_filter function.

Seems like you don’t understand the database… Platforms create the entity_ids. Recorder only includes or excludes entities (entity_ids) not platforms. If you want to exclude entity_ids from a platform, you have to exclude all entities created from said platform.

I know that I can include/exclude by entity_id or domain.
But I’m looking at source code and I’m trying to figure out a way to add include/exclude by platform functionality.

Last I checked, there’s no way to infer which platform created entity_id’s. The state machine does not store that information. Logger has it because that’s how python logging is built.

EDIT: Just to clarify

logger for python has a built in method that can track which methods were used. You can simply pass a format to logger and it will output this information in the logged line. That’s what home assistant is doing. Home assistant isn’t actively tracking which platform is logging what.

HA is using _LOGGER = logging.getLogger(__name__) and according to docs (https://docs.python.org/3/library/logging.html) __name__ is the module name.

I need to find a way to know what platform specific entity_id belongs to.
For example:

binary_sensor:
  - platform: rpi_gpio
    pull_mode: UP
    ports:
      12: Move

this entry has binary_sensor.move as id.
I can get domain using split_entity_id function, but I don’t know how to get the platform name, in this case, it should be rpi_gpio. Having the domain I will be able to modify generate_filter function and add include and exclude platforms.

I think that having such an option would allow us to exclude platforms like yr, time_date or command_line from recorder or history panel.

I’ll search a bit more and ask on Discord for hints. I think this would be a useful feature. WDYT?

It would be very useful for this and many other integrations. But there appears to be no way to get that information at the moment.

So that’s something that must be added before my changes can be added :slight_smile: