State of recorder component

I have the recorder component setup to use an external mysql server on my FreeBSD server, this works great/fine.

In the event of an outage my Pi with HomeAssistant on it starts up a lot lot faster so this means when/if that happens home assistant has started up and the recorder component is in a failed state.

I have tried looking for something, anything to tell me the state of the recorder component or history/logger component which rely on the recorder component but I can not find the state that I can make an automation against so I can get this to auto heal when necessary. Short of making a scraper/something to parse the html of the overview page does anyone have a solution to this or should I make a feature request?

THanks

I have a similar problem if there’s a power failure (or something) and ha reboots before the router I lose some components.

I was thinking about trying to get a notification if a persistent notification card is present, and/or automatically reboot a few minutes later when the Internet connection is up, but haven’t actually looked into trying to do it yet.

Food for thought.

I ran into a similar issue when I recently upgraded to Hassbian from AIO. I never added the recorder component back into the configuration file and didn’t realize for several hours.

Seeing your post, I cobbled together a small python script that would pull the most recent database entry from the MySQL states table, compare against current time and return the delta in seconds or minutes. It works great from a SSH window (even runs inside the venv), but HA won’t execute the script as a command line sensor. Seems to be an issue trying to import MySQLdb.

I’m trying to decide whether or not to make it run as a cronjob and have it feed the results to HA via the API…

Here is the script in case you want to play aroud with it:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import MySQLdb
import sys
import datetime

#Get last time the states table was updated in MySQL

db = MySQLdb.connect('sql_server_ip', 'username', 'password', 'homeassistant')

cursor = db.cursor()
cursor.execute("SELECT last_updated FROM states ORDER BY last_updated DESC LIMIT 1")

data = cursor.fetchall()

for row in data:
 last_updated = row[0]

db.close()


#Compare last record to current time and print delta in minutes

now = datetime.datetime.now() #timezone.utc)
diff = now - last_updated

seconds = diff.seconds
minutes = int(seconds / 60)
print (minutes)
1 Like

Thanks for the idea. I’m using sqlite still so modified your code.

On my Docker install of HASS this script will then run as a command line sensor.

For now I have HASS notify me if nothing gets updated for more than 2 minutes. If that seems to robust I’ll get HASS to automatically reboot at that time.

Yeah, I had to modify it also to handle the fact that my server is in utc0.

#!/usr/bin/env python

import sys
import datetime
from datetime import timedelta
import MySQLdb
import time

#Get last time the states table was updated in MySQL

db = MySQLdb.connect('192.168.11.10', 'asdf', 'asdf', 'asdf')

cursor = db.cursor()
cursor.execute("SELECT last_updated FROM states ORDER BY last_updated DESC LIMIT 1")

data = cursor.fetchall()

for row in data:
 last_updated = row[0]

db.close()

#Compare last record to current time and print delta in minutes
now = datetime.datetime.now() #timezone.utc)
tz_offset = int(time.localtime().tm_gmtoff / 60 / 60)
last_updated_in_this_tz = last_updated + timedelta(hours=tz_offset)
diff = now - last_updated_in_this_tz

seconds = diff.seconds
minutes = int(seconds / 60)
print ("the difference " + str(minutes))
1 Like