Skip to content

Instantly share code, notes, and snippets.

@DavidMStraub
Created August 2, 2016 15:30
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavidMStraub/6f1f64f8a5f191592efe6778a37a9348 to your computer and use it in GitHub Desktop.
Save DavidMStraub/6f1f64f8a5f191592efe6778a37a9348 to your computer and use it in GitHub Desktop.
# ...
shell_command:
clean_db: python3 /path/to/purge_sensor.py
automation:
- trigger:
platform: time
after: '03:00:00'
action:
service: shell_command.clean_db
import sqlite3
DB_PATH = '/path/to/home-assistant_v2.db'
ENTITY_IDS = ['sensor.my_sensor1', 'sensor.my_sensor2']
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
for eid in ENTITY_IDS:
c.execute("DELETE FROM events WHERE event_data LIKE ?", ('%\"entity_id\": \"' + eid + '\"%',))
c.execute("DELETE FROM states WHERE entity_id = ?", (eid, ))
conn.commit()
conn.execute("VACUUM")
conn.close()
@fabaff
Copy link

fabaff commented Aug 3, 2016

Home Assistant has the option to run scripts. Perhaps it worth to think about an integration of this. That way one could run it like hass --script purge-db sensor.my_sensor1.

@DomoticLab
Copy link

By running "shell_command.clean_db" where specific the name of the sensor ?? Thank

@SmartM-ui
Copy link

Hi,
with this script is it possible to delete the data of a sensor in a MariaDB database?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment