IOTstack

Table of Contents

Intro

Many of my smart home programs run on a Pi4 with Docker

Found IOTstack and used it to install docker and most of these programs in docker containers.  

Link above is for the newer version, I was using the older version but converted to latest in mid 2022.

Restoring NR Pi

Make copy of image file on uSD card, and run in pi

Login to pi. 

Check what docker containers are running

docker ps

Nothing was running, so  fix docker

cd IOTstack

sudo docker-compose up -d

cycle power to restart

Fix amazon.  Change ip address of alexa account in alexa node.

Go to ip address it says, and log in to get cookie.  Check and alexa should be working in NR now


Stack Maintenance & Updates

How to periodically update containers to keep things current.  Its easy, just takes a little bit.

Link


Stop programs first, I use Portainer to stop them.

Update most of the containers

cd ~/IOTstack

docker-compose pull

docker-compose up -d

Restart the pi, then prune to cleanup

sudo reboot 

docker system prune

Last update 8/16/2022

For MariaDB and Mosquitto, 

To check status of containers in docker

docker ps

9955946b4dba   iotstack_mariadb 

3e311162f5ae   iotstack_mosquitto 


When a newer version of the base image appears on DockerHub, you need to rebuild like this:

cd ~/IOTstack

docker-compose build --no-cache --pull «container»

docker-compose up -d «container»

docker system prune

docker system prune


cd ~/IOTstack

docker-compose build --no-cache --pull 9955946b4dba

docker-compose up -d 9955946b4dba

docker-compose build --no-cache --pull 3e311162f5ae

docker-compose up -d 3e311162f5ae

docker system prune

docker system prune


Install

IOTStack home

Make sure OS is up to date

sudo apt update

sudo apt upgrade

sudo reboot

Installed per these instructions for new automatic

Install curl, but may already be installed.

sudo apt install -y curl

Run the following commands:

curl -fsSL https://raw.githubusercontent.com/SensorsIot/IOTstack/master/install.sh | bash


Sudo reboot 


Install required systems patches

reboot at end of install.  I have bullseye so skipped middle step, and did the reset at end after running first command.

sudo bash -c '[ $(egrep -c "^allowinterfaces eth\*,wlan\*" /etc/dhcpcd.conf) -eq 0 ] && echo "allowinterfaces eth*,wlan*" >> /etc/dhcpcd.conf'


echo $(cat /boot/cmdline.txt) cgroup_memory=1 cgroup_enable=memory | sudo tee /boot/cmdline.txt


sudo reboot


Run the menu:

cd ~/IOTstack

./menu.sh

Select "Build Stack" from menu and pick the programs you want to install 


My list of programs below.   You can run again later, and pick new programs to add if you want.  

List of programs.  Select hamburger (3 hor bars) in upper left, them pick program you want to see.

Grafana left default of port 3000

Home Assistant (Container version, not supervised version, so no add ons)

InfluxDB

Mariadb  note:  There is setup to do before starting stack.  Build stack then see instructions in link

Mosquitto

PortainerCE

Python did not install, had an issue

Node red       installed on a different pi, not main one all other stuff runs on

Make sure you go to options and build an add on list, or deselect checkmarks.  Won't install NR if you don't have something for the add on list

Push enter to build stack

Exit program to get back to normal prompt.

This is what you should do before installing the programs.

Fix time zone

sudo nano docker-compose.yml

Find all the instances of - TZ and change to your time zone:

      - TZ=America/Los_Angeles


Now install everything

sudo docker-compose up -d

To check status of containers in docker

docker ps


Changes 

If you change docker-compose.yaml


down wll stop everything, and remove

docker-compose down


rebuild and restart

docker-compose up -d


Ports

ESPHome 6052

grafana 3000

home assistant 8123

influx db 2003, 8083, 8086

mqtt 1883, 9001

node red 1880

portainer 9000

zigbee2mqtt 8080

zwave2mqtt    8091

zwavejs2mqtt    8092, 3001


Mosquitto

link for info and instructions.

MariaDB

Setup for HA Revised 5/2022

Install MariaDB with IOTstack.


Fix MariaDB username and password per these instructions

Stop the container and remove the persistent storage area:

cd ~/IOTstack

docker-compose rm --force --stop -v mariadb

or use portainer to stop mariadb

sudo rm -rf ./volumes/mariadb

Edit docker-compose.yml and change the variables.

Bring up the container:

docker-compose up -d mariadb


Now you can use mariadb with updated password

cd ~/IOTstack/services/mariadb


docker exec -it mariadb bash

This starts different terminal.  Connect to database

mysql -uroot -p

And enter password


Yet another new terminal will start, with prompt
MariaDB [(none)]> 

Create a database called home_assistant

CREATE DATABASE home_assistant;


You can run  this to see all databases

show databases;

If you exit MariaDB terminal and re-enter, run this to get back into the database

USE home_assistant;

Response is
MariaDB [home_assistant]> 

Now make a user, and setup their password

CREATE USER IF NOT EXISTS hass_user IDENTIFIED BY 'PASSWORD';

The above didn't work, so I used following and it worked fine.  'passwordyouchoose' is what you need to enter in HA configuration.yaml to connect to database.

create user hass_user identified by 'passwordyouchoose';

To see all users, 

SELECT User FROM mysql.user;

Give privileges

GRANT ALL PRIVILEGES ON home_assistant.* TO hass_user;

Save what you just did

FLUSH PRIVILEGES;

Check to make sure it worked

SHOW GRANTS FOR hass_user;

It will show something like this.

Note that this does NOT contain the actual password.  Its a hash for the password

| GRANT USAGE ON *.* TO 'hass_user'@'%' IDENTIFIED BY PASSWORD '*LONG NUMBER' |

| GRANT ALL PRIVILEGES ON `home_assistant`.* TO 'hass_user'@'%'  


MISC Instructions and info


To delete user

DROP USER hass_user;

To exit terminal

exit or cntl C

Other commands/comments:


SELECT PASSWORD('password');

If the prompt changes after a command, and is not longer in the form of 

MariaDB [home_assistant]> 

Then it did not like your command format, and did not accept.  

Control C will exit out


Back at normal pi prompt, to get the  ip address for mariadb

docker inspect mariadb | grep IPAddress

 "IPAddress": "172.19.0.2",

Use the ipaddress to add this to config.yml

recorder:

  db_url: mysql://hass_user:PASSWORD@172.19.0.2/home_assistant?charset=utf8


or this also worked 


recorder:

  db_url: mysql://hass_user:PASSWORD@127.0.0.1/home_assistant?charset=utf8

PASSWORD is 'passwordyouchoose' from setting up the database

Influx DB and Grafana

Updated 6/2022

Grafana link   InfluxDB link

Install both with IOTStack

InfluxDB and Grafana docker-compose.yml

  influxdb:

    container_name: influxdb

#    image: "influxdb:2.2"

    image: "influxdb:1.8"

    restart: unless-stopped

    ports:

    - "8086:8086"

    environment:

    - TZ=America/Los_Angeles

    - INFLUXDB_HTTP_FLUX_ENABLED=false

    - INFLUXDB_REPORTING_DISABLED=false

    - INFLUXDB_HTTP_AUTH_ENABLED=false

    - INFLUXDB_MONITOR_STORE_ENABLED=FALSE

    - INFLUXDB_DATA_QUERY_LOG_ENABLED=false  #added

    - INFLUXDB_HTTP_LOG_ENABLED=false        #added

    volumes:

    - ./volumes/influxdb/data:/var/lib/influxdb

    - ./backups/influxdb/db:/var/lib/influxdb/backup


  grafana:

    container_name: grafana

    image: grafana/grafana

    restart: unless-stopped

    user: "0"

    ports:

    - "3000:3000"

    environment:

    - TZ=America/Los_Angeles

    - GF_PATHS_DATA=/var/lib/grafana

    - GF_PATHS_LOGS=/var/log/grafana

    - GF_AUTH_ANONYMOUS_ENABLED=true  #added

    - GF_SECURITY_COOKIE_SAMESITE=none  #added

    - GF_SECURITY_ALLOW_EMBEDDING=true  #added


    volumes:

    - ./volumes/grafana/data:/var/lib/grafana

    - ./volumes/grafana/log:/var/log/grafana

Stop Grafana  and InfluxDB with Portainer


Stop the container and remove the persistent storage area (similar to mariadb when changing docker-compose):

cd ~/IOTstack

docker-compose rm --force --stop -v influxdb

docker-compose rm --force --stop -v grafana

or use portainer to stop mariadb instead of force stop commands above


sudo rm -rf ./volumes/influxdb

sudo rm -rf ./volumes/grafana


Edit docker-compose.yml in ~/IOTstack

Reducing flash wear-out, add to environment section of influxdb

environment:

    - INFLUXDB_DATA_QUERY_LOG_ENABLED=false

    - INFLUXDB_HTTP_LOG_ENABLED=false

 

added anonymous enabled line for grafana.  Not sure if user and password are required

environment:

    - INFLUX_USERNAME=user

    - INFLUX_PASSWORD=password

    - TZ=Etc/UTC

    - GF_PATHS_DATA=/var/lib/grafana

    - GF_PATHS_LOGS=/var/log/grafana

    - GF_AUTH_ANONYMOUS_ENABLED=true  #added

    - GF_SECURITY_COOKIE_SAMESITE=none  #added

    - GF_SECURITY_ALLOW_EMBEDDING=true  #added


 

https://community.grafana.com/t/unable-to-display-a-dashboard-in-a-iframe-or-a-browser/51905/3 

GF_SECURITY_COOKIE_SAMESITE to none or disabled

and

GF_SECURITY_ALLOW_EMBEDDING to true


Bring up the containers:

docker-compose up -d influxdb

docker-compose up -d grafana

InfluxDB Setup

https://community.home-assistant.io/t/complete-guide-on-setting-up-grafana-influxdb-with-home-assistant-using-official-docker-images/42860

Start influx terminal

docker exec -it influxdb influx


CREATE DATABASE home_assistant

SHOW DATABASES

name: databases

name

----

home_assistant


show retention policies on home_assistant

name    duration shardGroupDuration replicaN default

----    -------- ------------------ -------- -------

autogen 0s       168h0m0s           1        true


I decided to save data in Influxdb for 2 weeks, so run the following command

alter retention policy "autogen" on "home_assistant" duration 2w shard duration 1w replication 1 default


Now check to make sure it worked

show retention policies on home_assistant


name    duration shardGroupDuration replicaN default

----    -------- ------------------ -------- -------

autogen 336h0m0s 168h0m0s           1        true





EXIT



To lookup a value from terminal:

docker exec -it influxdb influx

USE home_assistant


SELECT * FROM "°F"

SELECT * FROM "%";



Had to create a template in sensor.yaml so I could record thermostat temps

- platform: template

  sensors:

    temperature_thermostat_upstairs:

      friendly_name: "Upstairs Temp Thermostat"

      value_template: "{{ state_attr('climate.downstairs_thermostat','current_temperature') }}"

      unit_of_measurement: "°F"

      device_class: temperature


In Home assistant, edit configuration.yaml and add this.

I made a bunch of exclusions to try to limit recording size.


influxdb:

influxdb:  

  host: 192.168.xxx.xxx

  port: 8086

  database: home_assistant

  max_retries: 3

  default_measurement: state

  exclude:

    entities:

       - alarm_control_panel.home_alarm

       - sensor.openweathermap_cloud_coverage_2

       - sensor.openweathermap_condition_2

       - sensor.openweathermap_dew_point_2

       - sensor.openweathermap_feels_like_temperature_2

       - sensor.openweathermap_forecast_cloud_coverage_2

       - sensor.openweathermap_forecast_condition_2

       - sensor.openweathermap_forecast_precipitation_probability_2

       - sensor.openweathermap_forecast_pressure_2

       - sensor.openweathermap_forecast_temperature_2

       - sensor.openweathermap_forecast_temperature_low_2

       - sensor.openweathermap_forecast_time_2

       - sensor.openweathermap_forecast_wind_bearing_2

       - sensor.openweathermap_forecast_wind_speed_2

       - sensor.openweathermap_humidity_2

       - sensor.openweathermap_precipitation_kind_2

       - sensor.openweathermap_pressure_2

       - sensor.openweathermap_rain_2

       - sensor.openweathermap_snow_2

       - sensor.openweathermap_temperature_2

       - sensor.openweathermap_uv_index_2

       - sensor.openweathermap_weather_2

       - sensor.openweathermap_weather_code_2

       - sensor.openweathermap_wind_bearing_2

       - sensor.openweathermap_wind_speed_2

    domains:

       - binary_sensor

       - light

       - switch

       - weather

       - zone

  tags:

    instance: prod

    source: hass

Grafana Setup


From a browser, access grafana UI

192.168.xxx.xxx:3000

login with user = admin

pw = admin

it will have you change the pw

Select data source, then InfluxDB

Enter URL http://192.168.xxx.xxx:8086

Enter InfluxDB database name as home_assistant

See screen caps below.

Save and Test

Grafana Use

Create New Dashboard, then add new Panel

Give Panel a title on right side

Figure out time scale you want, I am using 3 days.  After you make the first panel, all the rest in the dashboard will default to the same timescale.

On From Row towards bottom, Click on Select Measurement, then choose °F for example. 
Click + after WHERE then select entity_id, then click select tag value then pick what you want to chart.  

Click save in top right when done to save dashboard, then apply.

Click + icon on top to add another panel.  Make like first one, then apply to add it

I had trouble seeing the entities at the bottom.  Picking Explore on the left pulls query up in a larger window, makes it easier to find entries.  

Be sure to do the step, click on Select Measurement, then choose °F for example.   If you don't do this, graphs don't show up.

To select an identity, you can type the beginning of it's name, and that will narrow down the ones in the pick list.

Insert a graph

Click panel title, then share.

Pick Embed from options across top.  Turn off Lock time range

Copy http part of stuff inside the box

http://192.168.xxx.xxx:3000/d-solo/Sw8UWxjnk/test?orgId=1&panelId=5" width="450" height="200" frameborder="0

You can delete the stuff at the end, and use this format

http://192.168.xxx.xxx:3000/d-solo/ijFfehCnk/temp_3d?orgId=1&panelId=4

or

http://192.168.xxx.xxx:3000/d-solo/ijFfehCnk/temp_3d?orgId=1&panelId=2


In Home Assistant, add a card in lovelace editor, then pick webpage card.

Copy http line into URL section, and give it a name.  Should show up now.