Skip to content

Instantly share code, notes, and snippets.

@DavidMStraub
Created August 2, 2016 15:30
Show Gist options
  • 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()
@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