Skip to content

Jupyter Lab in Home Assistant

TonyM1958 edited this page Aug 7, 2023 · 8 revisions

Jupyter Lab is an easy to use, web-based interactive development environment that lets you play around with code and data within a notebook format. It's flexible interface allows you to perform simple programming tasks using Python and extends to complex data and scientific anaylsis and machine learning:

Part of the power of Jupyter Lab is that you can download and use libraries of code written by other people, configure this and run it to get data specific to you, such as Solcast solar forecast information:

Or, access the Fox Cloud API to read data about your inverter:

Finally, Jupyter Lab can be as a Home Assistant Add-on, so you can do all of this through the same web interface you use to monitor your inverter.

Installing Jupyter Lab in Home Assistant

Just a warning before you leap in - Jupyter Lab is a reasonably large lump of code - it's best to make sure you have enough processor, memory and disk head room on your HA host before installing. If you are already struggling, this is probably not for you?

  • Go to Settings, Add-ons and click the ADD-ON STORE button
  • Search for the 'Jupyter Lab' add on:

  • Click Install and wait for it to finish
  • Turn on 'Show in side bar'
  • Click 'OPEN WEB UI'

You can now create or add notebooks to Jupyter Lab. Notebooks are created in the folder 'notebooks' inside your HA CONFIG folder. Jupyter Lab includes a file explorer that starts in this folder:

Using Packages

Packages are software libraries, ready built, that you can use within Jupyter Lab. This makes it a lot easier to write things as you can import someone's code and just use it. In this example, the Jupyter notebook you can see is the file 'forecast.ipynb':

The second and third lines in the first cell are:

import private as my
import solcast as s

These import Python files and allows you to call the functions provided. In this case, 'private' refers to the file 'private.py' that is in the same folder as the notebook and contains your settings for the Solcast web site. 'solcast' refers to the file 'solcast.py' that contains code for accessing the Solcast web site and downloading and processing forecast data. File names are case sensitive (this.py and THIS.py are different files).

The line:

s.solcast_settings(api_key=my.solcast_api_key, rids=my.rids, debug=1)

Uses these files to load your Solcast api key and the rids for the solar arrays. Then:

f=s.Solcast()

creates a 13 day solar estimate and forecast using your parameters and returns the results and saves them in the local variable f. Then:

print(f)

prints the forecast (the text below the cell), and

f.plot_daily()

uses the forcast to plot the graph.

Adding System Packages

As well as local packages, there are lot of published 'system' packages that can be used with Python and Jupyter Lab. These provide simple things like time and date functions and maths operations through to complete machine learning systems. Lots of packages can be found and installed using pip

Typically, if you need to install a system package, this will be noted as part of the package configuration.

To add system packages to Jupyter Lab:

  • Go to Settings, Add-ons and open Jupyter Lab
  • Go to the Configuration tab

  • To add a package, go to init_commands and enter the command line to install the package. For example, this package is required to access the Fox Cloud API:
pip install random-user-agent
  • after adding this, restart the Jupyter Lab add-on and check the logs

Tips

Each time you navigate to or from the Jupyter Lab tab in HA, the add-on will restart and may or may not have saved your last notebook when it does this. This is painful if you are editing files in Studio Code Server and running them in Jupyter Lab. I tend to open two browser windows and set one to Jupyter Lab and use the other for code development and monitoring. If you have enough screen space, open as many HA instances as you want and set each one to look at different tabs.