This notebook contains a simple example that creates a graph with data stored by Home Assistant in the local database. For a introduction to matplotlib check the matplotlib - 2D and 3D plotting in Python notebook.
# matplotlib for plotting the data
%pylab inline
Populating the interactive namespace from numpy and matplotlib
# Instead of SQLAlchemy is the built-in Python support for SQLite used.
import sqlite3
import datetime
from matplotlib import dates
conn = sqlite3.connect('/path/to/.homeassistant/home-assistant_v2.db')
data = conn.execute("SELECT state, last_changed FROM states WHERE entity_id = 'sensor.random' AND state != 'unknown'")
values = []
timestamps = []
for x in data:
timestamps.append(dates.date2num(datetime.datetime.strptime(x[1], '%Y-%m-%d %H:%M:%S.%f')))
values.append(float(x[0]))
plt.plot_date(x=timestamps, y=values, fmt="r-")
plt.ylabel('Values')
plt.xlabel('Time line')
<matplotlib.text.Text at 0x7efe21680748>