Automation delete file know device

Hello, flying to know if anyone can help me with an automation. I have to empty or delete the know devices file every 3 days. Thank you in advance!

The first question is … why?

That said, you’d need to do that from a shell script, and restart Home Assistant afterwards. How you do those depends on how you installed Home Assistant.

the need is because in b & b people connect to WiFi. thus the devices become presence sensors. the cleaning lady will know when they have left. With the passage of time, these devices are increasing more and more and clogging the rows of sensors. that’s why I would like to automate the file emptying. But you can create a file by restarting HA

Then… what install method did you use? Hass.io, Hassbian, Docker, one of the community guides (which one?), something else?

I use a shell script to delete everything below a ‘flag’ line in known_devices.yaml… I make sure everything I want to keep is above the flag otherwise it tends to fill up with spurious stuff… depends on which device trackers you use. I define the bash script as a shell command and let an automation call that every time home assistant starts which keeps it under control.

Hi David - are you able to share that script. I would like to be able to do the same thing as the OP. I’m not expert at Linux so hoping to have something to start fro, rather than googling it all. Any help appreciated :slight_smile:

add a shell_command:

shell_command:
  clean_knowndevices: /bin/bash /config/clean_knowndevices.sh

add an automation that runs it every time Home Assistant starts:

- id: 'cleanknowndevices'
  alias: Clean out Known Devices
  trigger:
  - event: start
    platform: homeassistant
  action:
  - service: shell_command.clean_knowndevices

In the file known_devices.yaml add this line after all devices you want to keep:

#end-of-my-devices#

Then finally, in the same folder as your configuration.yaml, create clean_knowndevices.sh and add these contents:

#!/bin/sh
/bin/sed -i '/#end-of-my-devices#/q' /config/known_devices.yaml

You will then need to use ssh in that directory to make it executable…
from ssh:

chmod +x clean_knowndevices.sh

Then it will run the script everytime home assistant starts.

3 Likes

Deleting entities is something that I have done several times. My situation is that I have my system set to auto-discovery and I primarily use MQTT (hosted locally) for integration.
As I configure my own IoT sensors and relays, sometimes they are “grabbed” by the system before I have given them a proper ID (in the form of a MQTT topic) and the result is several phantom devices lurking around in HA. So I decided to flush the whole thing five days ago and let HA re-discover which devices are actually active. Turned out to be a bad decision since this torpedoed all my automations. Now if I open the automations, the device information has be reset - even though the devices are now back on line. I can’t figure out if this is a bug or a feature but I am getting more and more nervous about trusting the HA automations to do anything important. I am considering using my own Node-Red flows or even just write my own automation engine that would sit next to HA (on a different computer) and take care of important automations.
Long story short: “If devices are deleted (even if only temporary) should the automations be damaged?” Would a sensible alternative not be to “disable” the automation in case any of the devices or entities are not found?

If you’re finding that the entities come back with the same Entity and Device ID and the device automations don’t work, please open an issue.

Thank you for the recommendation. I will do some testing first to verify that this really is the case. It is good though to get a confirmation that this is NOT the way an Automation should behave. To be continued…