Data Science quick start guide for non-hass.io users

Here you find a collection of quick start guides for the Home Assistant data science lab for different platforms other than hass.io.

9 Likes

For those who want to seperate datascience and home automation on seperate systems, i would recommend to install it on your computer for testing. Here simple steps to set it up. (5 min)
This assumes you already have python installed on your machine. This procedure could also be used with small modifications on your existing home assistant virtual env on the pi or other OS.

Data environment on Windows

1 Create a directory for virtual data environment

Start Command prompt (in administrator mode):

Create directory for virtual environment:

mkdir c:\hassdata

2 Create virtual pyton environment

$ virtualenv c:\hassdata

3 Activate virtual environment

$ c:\hassdata\scripts\activate

Will show:

(hassdata)$

4 Install jupyterlabs

(hassdata)$pip install jupyterlab

5 Install hass datascripts

(hassdata) $pip install HASS-data-detective

6 Run jupiterlabs

(hassdata) $ jupyter lab

3 Likes

I threw together a Dockerfile that is auto building to https://hub.docker.com/r/kylerw/hass-data-detective for those that want to take advantage of it.

I have this running on unRAID in Bridge mode and map

Volumes

/etc/localtime:/etc/localtime:ro
/mnt/cache/appdata/jupyter:/home/jovyan 
/mnt/cache/appdata/home-assistant:/hass-config:ro

Port
8888:8888

and

Variable

JUPYTER_ENABLE_LAB:yes

screenshot of the Docker setup:

then in the Jupyter Notebooks I use:

from detective.core import db_from_hass_config
db = db_from_hass_config("/hass-config")

to detect the database I’m using (separate mariadb container)

On first run, you need to view the logs to get the token that is generated to log in and create a password.

5 Likes

Quick start on Ubuntu

Installation

Starting with a fresh Ubuntu 18.10.

sudo apt update
sudo apt upgrade
sudo apt install python3-pip
sudo pip3 install jupyterlab
sudo pip3 install HASS-data-detective
sudo pip3 install homeassistant
sudo pip3 install sqlalchemy

If using postgreSQL:

pip3 install psycopg2-binary

Prepare environment

su - ubuntu
mkdir ~/Notebook
cd ~/Notebook
git clone https://github.com/home-assistant/home-assistant-notebooks.git

Launch Jupyter Lab as systemd service

sudo curl -o /etc/systemd/system/jupyterlab.service https://gist.githubusercontent.com/cgtobi/11439422a65610680127dcb2d507482a/raw/0ba0574bafc160c1cc9edf0d1361c7ae62af7453/jupyterlab.service
sudo systemctl daemon-reload
sudo systemctl enable jupyterlab.service
sudo systemctl start jupyterlab.service

Create read-only database user

PostgreSQL

Run where your HASS DB lives:

sudo -u postgres psql
postgres=# CREATE USER datascience WITH encrypted password 'my_secret_password';
sudo -u postgres psql -dhass
hass=# GRANT CONNECT ON DATABASE hass to datascience;
hass=# ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO datascience;
hass=# GRANT USAGE ON SCHEMA public to datascience; 
hass=# GRANT SELECT ON ALL SEQUENCES IN SCHEMA public TO datascience;
hass=# GRANT SELECT ON ALL TABLES IN SCHEMA public TO datascience;

Run the GETTING STARTED notebook

Open the GETTING STARTED notebook and modify the database connection accordingly.

db = HassDatabase("postgresql://datascience:[email protected]/hass")

Now follow the instructions in the notebook, click on “Run” in the top menubar and click on “Run All Cells”.

See the quick start guide.

3 posts were split to a new topic: Setting up postgres

Hi,

Thanks for these instructions.

I notice you are installing home assistant. Is this required simply because HASS-data-detective somehow depends on a part of it or is this the “actual” Home-Assistant installation ?

I have my main homeassistant installed on a difference machine to where I want to run the data science stuff, so wondering if I can skip the “sudo pip3 install homeassistant” step.

Thanks

You probably can do some stuff without it. But it does not interfere with your actual installation.