This notebook contains a simple example that creates a graph with data stored by Home Assistant in the local database. For the graph is mpld3 used which combines matplotlib and D3js.
# matplotlib for plotting the data
%pylab inline
Populating the interactive namespace from numpy and matplotlib
import datetime
import warnings
warnings.filterwarnings('ignore')
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import dates
import mpld3
mpld3.enable_notebook()
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from homeassistant.components.recorder.models import Base, Events, States, RecorderRuns
# Your database URL
DB_URL = "sqlite:////path/to/.homeassistant/home-assistant_v2.db"
# Database setup
engine = create_engine(DB_URL)
Base.metadata.create_all(engine)
session_factory = sessionmaker(bind=engine)
Session = scoped_session(session_factory)
data = list(engine.execute("SELECT state, last_changed FROM states WHERE entity_id = 'sensor.kitchen_temperature' AND state != 'unknown' AND state < 25"))
# Samples
data[0:4]
[('15', '2016-07-19 11:30:20.862355'), ('15', '2016-07-19 12:44:24.947883'), ('15', '2016-07-19 13:25:51.232524'), ('15', '2016-07-21 12:08:43.863633')]
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]))
# Sample
timestamps[0], values[0]
(736164.4794081291, 15.0)
labels = timestamps
plt.plot_date(x=timestamps, y=values, fmt="r-")
plt.grid(b='on')
plt.ylabel('Temperature')
plt.xlabel('Time line')
<matplotlib.text.Text at 0x7f2d5e32d9b0>
There is almost no difference to the first graphing notebook. A few graphing libraries can be used that way which allows you to choose the library that fits best.