Category: Coding

  • My Home Assistant setup

    My Home Assistant setup

    There are several ways to setup Home Assistant. It can run on a dedicated machine for example raspberry pi with the Home Assistant Operating System (HAOS). HA can also be run in a container environment on any pic you might have around. See setting up home assistant for more details.

    I had an 12 year old iMac which had its hard drive die. I set it up to boot into Ubuntu from an external SSD. This machine has enough power to run various tasks I need from Ubuntu. It can also to run Docker allowing HA to run in containers.

    Home assistant docker setup

    Home assistant has a docker container available which you can use to run it. However it takes a bit more work and add-ons to be set up in separate containers.

    I use a docker-compose.yml file to launch all the containers I need – at the moment, I have

    1. Home Asisstant
    2. mariadb as the database for HA
    3. mosquitto for MQTT. This is using a shared network so that I can send MQTT data from another container I start separately.
    4. zwavejs for zwave
    5. phpmyadmin – to keep an eye on the database
    6. nginx – to be able to login remotely to my HA setup

    You won’t need all these to get started, probably just HA and mariadb would be a good place to start. I’m documenting other services to give an idea of the level of technical know needed to use docker instead of the HAOS.

    The docker-compose file to start all these services looks like this

    # docker-compose.yml
    services:
      homeassistant:
        container_name: homeassistant
        image: "ghcr.io/home-assistant/home-assistant:stable"
        volumes:
          - /home/Documents/home_assistant/my_local_config:/config
          - /etc/localtime:/etc/localtime:ro
          - /run/dbus:/run/dbus:ro
    
        restart: unless-stopped
        privileged: true
        network_mode: host
        depends_on:
          - mariadb
        stdin_open: true
        tty: true
    
    
      mariadb:
        image: mariadb:latest
        container_name: mariadb
        restart: always
        environment:
          MYSQL_ROOT_PASSWORD: "${MYSQL_ROOT_PASSWORD}"
          MYSQL_DATABASE: hass_database
          MYSQL_USER: homeassistant
          MYSQL_PASSWORD: "${HASS_MYSQL_PASSWORD}"
          PUID: "${PUID}"
          PGID: "${PGID}"
        ports:
          - "3306:3306"
        volumes:
          - /home/Documents/home_assistant/mysql_data:/var/lib/mysql
    
      phpmyadmin:
        container_name: phpmyadmin
        image: phpmyadmin/phpmyadmin
        restart: always
        environment:
          PMA_HOST: mariadb
          PMA_USER: root
          PMA_PASSWORD: "${MYSQL_ROOT_PASSWORD}"
        ports:
          - "8080:80"
        depends_on:
          - mariadb
          
      zwavejs2mqtt:
        container_name: zwavejs2mqtt
        image: zwavejs/zwavejs2mqtt:latest
        restart: always
        tty: true
        stop_signal: SIGINT
        environment:
          - SESSION_SECRET=yourSuperSecretHere
          - ZWAVEJS_EXTERNAL_CONFIG=/usr/src/app/store/.config-db
        devices:
          - '/dev/serial/by-id/usb-Silicon_Labs_your_device-port0:/dev/zwave'
        volumes:
          - /home/Documents/home_assistant/z-wave_config:/usr/src/app/store
        ports:
          - "8091:8091" # port for web interface
          - "3000:3000" # port for Z-Wave JS websocket server
    
      npm:
        container_name: npm
        image: 'jc21/nginx-proxy-manager:latest'
        restart: unless-stopped
    
        ports:
          # These ports are in format <host-port>:<container-port>
          - '80:80' # Public HTTP Port
          - '443:443' # Public HTTPS Port
          - '81:81' # Admin Web Port
    
    
        environment:
          TZ: "Australia/Brisbane"
    
        volumes:
          - /home/Documents/home_assistant/nginx/data:/data
          - /home/Documents/home_assistant/nginx/letsencrypt:/etc/letsencrypt
          - /home/Documents/home_assistant/nginx/static_site:/static
          
      mosquitto:
        image: eclipse-mosquitto
        container_name: mosquitto
        restart: unless-stopped
        volumes:
          - /home/Documents/home_assistant/mosquitto:/mosquitto
          - /home/Documents/home_assistant/mosquitto/data:/mosquitto/data
          - /home/Documents/home_assistant/mosquitto/log:/mosquitto/log
        ports:
          - 1883:1883
          - 9001:9001
        networks:
          - my_shared_network
          
    networks:
      my_shared_network:
        external: true
        

    I stored the passwords in a .env file in the same directory as the docker-compose.yaml file. Docker compose automatically loads this file if it exists and sets the environment variables.

    # .env file
    MYSQL_ROOT_PASSWORD=MyReallySTrongPassword
    HASS_MYSQL_PASSWORD=AnotherGoodPassword
    PUID=1000
    PGID=1000

    To start all the containers just run the command.

    $ docker compose up -d

    You can monitor that everything is working by issuing the docker ps command.

    docker ps --format "table {{.Names}}\t{{.Image}}\t{{.Status}}"
    NAMES           IMAGE                                          STATUS
    npm             jc21/nginx-proxy-manager:latest                Up 11 days
    docker2mqtt     docker2mqtt-docker2mqtt                        Up 11 days
    phpmyadmin      phpmyadmin/phpmyadmin                          Up 11 days
    homeassistant   ghcr.io/home-assistant/home-assistant:stable   Up 11 days
    mariadb         mariadb:latest                                 Up 11 days
    mosquitto       eclipse-mosquitto                              Up 11 days
    zwavejs2mqtt    zwavejs/zwavejs2mqtt:latest                    Up 11 days
  • Using home assistant for monitoring battery and solar

    Using home assistant for monitoring battery and solar

    We have been using home assistant for monitoring and controlling many items in our home for a few years. This is a fantastic open source home automation system, and is ideal for monitoring energy usage. It can also be used for controlling use and charging of a battery taking into account tariffs, solar input. We also plan to use it to monitor and control the HVAC system and the hot water heat pump.

    We considered a number of different solutions for battery storage, and decided to go with a Tesla Powerwall 3 solution. This is a mature and well tested system, even if we had some worries about the CEO. Our system has two Powerwalls giving 27.5 kWh of storage along with a gateway. This setup gives us whole home backup, and the inverter is built into the Powerwall.

    Home assistant has a pretty good setup for monitoring energy usage documented at HA energy dashboard. The dashboard needs to be able to read out the information from the inverter, battery, grid usage, and solar production. Many battery and inverter systems are able to be integrated with home assistant in a straightforward manor. The Tesla Powerwall app which allows the user to control and readout the battery is a little more locked down.

    Getting access to the Powerwall data

    There are however several ways to get this data into home assistant.

    1. Locally read the data by connecting to the wifi which is on the Powerwall.
    2. Access Tesla’s developer API.
    3. Use a paid third party solution to access the Tesla API and get up and running quickly.

    I explored, and managed to get all three of these options working, but in the end I am using the third option with my HA setup. It works flawlessly, is quite affordable, and prepares all the sensors needed to setup the energy dashboard in home assistant. The downsides of this approach are that it depends on access to the Tesla cloud, and costs a small amount of money.

    If you are interested in either of the first two options, I recommend having a look at the excellent pypowerwall python library https://github.com/jasonacox/pypowerwall. This has code to access the data provided by the battery. There is also a wealth of information about how to connect to either the local wifi or the cloud.

    If you want to connect to the local wifi, be sure to take a photo of the inside of the Powerwall when it is being installed. There is a sticker in there which has the SSID : (something like TeslaPW_XXXXXX) and Password on it which you will need to connect to the wifi. You can connect a computer to this wifi, and then can use the TEDAPI in pypowerwall to access the local data from your battery. This gives access to information like the power on each string of PV panels.

    The quick solution – using Teslemetry

    There are a lot of complications in getting this data into home assistant, but if you have enough IT skills it is doable. If you want to connect to the Tesla API without paying, you need to register on the Tesla portal (https://developer.tesla.com). This allows you to make calls and get information about your battery without paying. Again, if you have sufficient IT skills, you can do this. I set up an account, and managed to make the API calls, but opted for the easier solution in the end.

    Using the Telslemetry solution https://teslemetry.com makes it incredibly easy to integrate the data from the battery into home assistant, and costs only a few dollars a month. In the end it was worth it to subscribe, and be up and running reliably and immediately rather than spending a lot of time rolling my own solution.

    After subscribing to Teslemetry, and adding the integration into home assistant, I was able to setup the home assistant energy monitoring with the sensors defined by Teslemetry.

    Setting up the Home Assistant energy dashboard

    When you setup your Powerwall with the Tesla app, you give the system a name. You will need to edit the energy dashboard in HA, and enter 6 sensors. The sensors which are available and wher to enter them in the dashboard are (for a:

    1. My system Grid Imported (add to Electricity Grid/Grid Consumption)
    2. My system Grid Exported (add to Electricity Grid/Return to Grid)
    3. My system Grid Power (add to Electricity Grid/Grid Power)
    4. My system Battery Charged (Home battery storage/Battery systems)
    5. My system Battery discharged (Home battery storage/Battery systems)
    6. My system Solar generated (Solar panels/ Solar production)

    After doing this, there is lots of information provided by the energy dashboard in home assistant.