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?