Auto Delete MQTT devices after no activity for x days

I had RTL433 set a little too aggressive on protocols it was looking at, and the number of devices MQTT is seeing for 433 is over 350. Every time I move my RV (where Home Assistant resides), it picks up 100s more as I travel. From TPMS sensors (now disabled) and weather stations, etc.

When I put the question above (the topic title) into Google trying to find an answer, the AI answer gave me this script for automation. But how to get it to go through and cycle through the devices… that I have no clue.

  - alias: "Delete Inactive MQTT Devices"
    trigger:
      - platform: time
        at: "03:00:00"  # Run daily at 3am
    condition:
      - condition: template
        value: "{{ state_attr('your_mqtt_device_entity', 'last_updated') | timestamp_custom('%Y-%m-%dT%H:%M:%S') |  as_timestamp < (now() | timestamp_custom('%Y-%m-%dT%H:%M:%S') | as_timestamp - (5 * 24 * 60 * 60)) }}"
    action:
      - service: homeassistant.remove_entity
        data:
          entity_id: "your_mqtt_device_entity" 

Is there a way to get an automation or a script to loop through all MQTT devices and check for last_updated and delete those that are older than say 5 days and do so automatically?

That is not a valid action.

The LLM was bullshitting you. Please stop using it. It was trained on old data and likes to make things up.

Valid home assistant actions are listed here: Home Assistant Core Integration - Home Assistant There is no (core) action to remove an entity.

Have a look at the 3rd party integration Spook, it has some advanced entity management: Entity management - Spook 👻 a scary powerful toolbox for Home Assistant.

Of that I understood that the LLM was bullshitting. But it got me to thinking if something along those lines MIGHT be possible. To cycle through all MQTT devices, and if last_updated was over 5 days old, then delete that entity.

I don’t understand if the cycling through all devices part is possible or not.

Nothing in your post indicates that.

As I said, maybe. Check out Spook.

Yes that is possible.