Created
August 2, 2016 15:30
-
-
Save DavidMStraub/6f1f64f8a5f191592efe6778a37a9348 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ... | |
shell_command: | |
clean_db: python3 /path/to/purge_sensor.py | |
automation: | |
- trigger: | |
platform: time | |
after: '03:00:00' | |
action: | |
service: shell_command.clean_db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
By running "shell_command.clean_db" where specific the name of the sensor ?? Thank
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
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
.