Kitchen Overview

Raspi model B used as a room controller and extension of the home assistant installation. Monitoring and controlling devices in the kitchen. Built from a Raspberry Pi Model B v1, some 5v relays and a few temperature sensors.

I’ve installed Raspbian OS on the pi and have loaded Room Assistant following this guide to handle the low level communication stuff back to my Home Assistant server.

Kitchen Info and Use

This device lives on top of our pantry freestanding unit that lives in our kitchen. Network cables were connected through the adjacent wall to the garage where I have a network switch.

Main Functions

Responsible for occupancy and temperature readings for one of the most used rooms in the house. The occupancy sensor is tied to a LIFX light above the stove through a home assistant automation to trigger the lights on motion detection.

Room temp is sent back to Home Assistant to be averaged into the whole house temperature used by the Furnace Controller.

Wiring

Everything wires to the GPIO pins of the Raspberry Pi using DuPont cables through a hole drilled into the case. Nothing too complex with this setup.

Kitchen raspi wiring diagram

Hardware

Raspberry Pi Model B v1 is the brain here, providing MQTT communication to the controller and reading sensor inputs via GPIO pins.

  • Motion Sensor (PIR)
  • Temp Sensor (DS18B20)
  • Temp/Humid (DHT11)
  • Input Switches (x2)
  • Relay Control (x2)

Network and Power

WiFi connection required here due to location and limited access to run wire. Simple to configure the WiFi USB dongle through the raspi-config command line utility. Once connected to the network, assign a static IP through the DHCP server for consistency and cluster settings.

Power is provided from the wall outlet below.

Controls

  • Relays for controlling LED’s or any other thing that comes later. Not currently used.
  • Input Switches for door switches or light switch. Not currently implemented

Temp Sensors

There are 2 sensors attached to this device. Since it is mounted up near the ceiling, the temp is swayed to a higher reading than the actual feel. Due to this I added an additional sensor that hangs down to head height.

Using the DHT11 sensor and a script to report the temp every min (cron job through room-assistant script.

I’m using the old depreciated Adafruit_Python_DHT library due to issues with the new circuit python version and already having this working. I didn’t feel like changing something that is working.

Using the DS18B20 sensor and a script to check the temp using python and a onewire connection.

Room-Assistant config excerpt:

shell:
  sensors:
     # DS18B20 Shelll Sensor
    - name: Kitchen Temperature 1
      command: 'python3.7 /home/pi/room-assistant/script/temperature_sensor_code.py'
      cron: '* * * * *'
      icon: mdi:temperature-fahrenheit
      unitOfMeasurement: '°F'
      deviceClass: temperature

     # DHT11 Sensor reporting the temp °F result
    - name: Kitchen Temperature
      command: 'python3.7 /home/pi/room-assistant/script/myDHT.py'
      regex: '(-?[0-9.]+)F'
      cron: '*/2 * * * *'
      icon: mdi:temperature-fahrenheit
      unitOfMeasurement: '°F'
      deviceClass: temperature

     # DHT11 Sensor reporting the humid % result
    - name: Kitchen Humidity
      command: 'python3.7 /home/pi/room-assistant/script/myDHT.py'
      regex: '(-?[0-9.]+)%'
      cron: '*/5 * * * *'
      icon: mdi:water-percent
      unitOfMeasurement: '%'
      deviceClass: humidity

Mounting

This device sits on top of the furniture, hidden from view tucked behind some decorative molding. Only thing that is visible is the small PIR motion sensor looking at the room.

Software

Everything is controlled and mapped to the home assistant dashboard over MQTT via the room-assistant system.

Room Assistant Config

This config file is the meat and potatoes of the program. Find it in /home/$USER/room-assistant/config/local.yml on the Raspberry Pi.

# ########################
# Raspi4 Config
#   Kitchecn control and monitoring
#   192.168.1.71
# ########################

# ############
# GPIO Pin-Out
# ############
#
# Inputs:
# Pin: 04 Use: humidity/temp DHT11 sensor using the shell functions below
# Pin: 24 Use: Kitchen switch 0
# Pin: 18 Use: Kitchen switch 1
# Pin: 22 Use: Motion
# Pin: 14 Use: DS18B20 temp sensor data pin
#
# Outputs:
# Pin: 23 Use: Kitchen relay 0
# Pin: 17 Use: Kitchen relay 1

# #######################
# Global Config settings
# #######################
global:
  instanceName: kitchen
  integrations:
    - homeAssistant
    - gpio
    - shell

# #######################
# Cluster settings
# #######################
# Config options for clustering multiple Room-Assistant
# Give leader more weight
cluster:
  weight: 1
#  networkInterface: eth0
  networkInterface: wlan0
  port: 6425
  timeout: 60
  peerAddresses:
    # raspi1 patio
    - 10.10.10.70:6425
    # raspi2 office
    - 10.10.10.71:6425
    # raspi3 sprinkler
    - 10.10.10.72:6425
    #raspi4 kitchen <-- this cpu
#    - 192.168.1.71:6425 # wired
#    - 192.168.1.75:6425 # wifi
    #raspi5 crawlspace
    - 10.0.0.74:6425
    # raspi6 garage
    - 10.10.10.73:6425

# #######################
# home assistant settings
# #######################
homeAssistant:
  mqttUrl: mqtt://10.10.10.21:1883
  mqttOptions:
    username: homeassistant
    password: {LONG_RANDOM_STRING}

# #######################
# GPIO settings
# #######################
gpio:
# Pin 4 has humidity/temp DHT11 sensor using the shell functions below
# switch for local kitchen stuff
  binarySensors:

    # 5v Relay breakout board
    - name: Kitchen Switch 0
      pin: 24
    
    # 5v Relay breakout board
    - name: Kitchen Switch 1
      pin: 18
    
    # PIR motion sensor
    - name: Kitchen Motion
      pin: 22
      deviceClass: motion

# #######################
# Switches settings
# #######################
  switches:

    # Relay 1
    - name: Kitchen Relay 0
      pin: 23
      icon: mdi:electric-switch
    
    # relay 2
    - name: Kitchen Relay 1
      pin: 17
      icon: mdi:electric-switch

# #######################
# Shell settings
# #######################
shell:
  sensors:

     # DS18B20 Shelll Sensor
    - name: Kitchen Temperature 1
      command: 'python3.7 /home/pi/room-assistant/script/temperature_sensor_code.py'
      cron: '* * * * *'
      icon: mdi:temperature-fahrenheit
      unitOfMeasurement: '°F'
      deviceClass: temperature

     # DHT11 Sensor reporting the temp °F result
    - name: Kitchen Temperature
      command: 'python3.7 /home/pi/room-assistant/script/myDHT.py'
      regex: '(-?[0-9.]+)F'
      cron: '*/2 * * * *'
      icon: mdi:temperature-fahrenheit
      unitOfMeasurement: '°F'
      deviceClass: temperature

     # DHT11 Sensor reporting the humid % result
    - name: Kitchen Humidity
      command: 'python3.7 /home/pi/room-assistant/script/myDHT.py'
      regex: '(-?[0-9.]+)%'
      cron: '*/5 * * * *'
      icon: mdi:water-percent
      unitOfMeasurement: '%'
      deviceClass: humidity

    # Script to check onboard temp sensor reading
    - name: Kitchen CPU Temp
      command: '/home/pi/room-assistant/script/cpuTemp.sh'
      cron: '*/2 * * * *'
      unitOfMeasurement: '°F'
      deviceClass: temperature
    
    # Script to check voltage and return boolean if no errors seen. 
    # See the script for more
    - name: Kitchen CPU Voltage
      command: '/home/pi/room-assistant/script/cpuVolt.sh'
      cron: '1 */1 * * *'
      deviceClass: power

    # Report CPU uptime in sec
    - name: Kitchen CPU Up Time
      command: '/home/pi/room-assistant/script/cpuUp.sh'
      cron: '* * * * *'
      unitOfMeasurement: 's'

    # Free memory `free -h` results
    - name: Kitchen CPU Free Memory
      command: '/home/pi/room-assistant/script/freeMem.sh'
      cron: '*/10 * * * *'
      unitOfMeasurement: 'MB'

    - name: Kitchen Wifi Strength
      command: 'iwconfig wlan0 | grep -i quality'
      regex: 'Signal level=(-?[0-9]+) dBm'
      cron: '*/30 * * * *'
      icon: mdi:wifi
      unitOfMeasurement: dBm
      deviceClass: signal_strength

#    - name: Kitchen CPU Free Storage
#      command: '/home/pi/room-assistant/script/cpuStorage.sh'
#      cron: '1 * */1 * *'
#      unitOfMeasurement: 'GB'
#      deviceClass: timestamp

# #######################
# Entity settings
# #######################
entities:
  behaviors:
    kitchen_motion_sensor:
      debounce:
        wait: 0.75
        maxWait: 2

Scripts

Some additional helper scripts were also developed to return info on the device for integrations and status’s over in home assistant.

Add these to room assistant using the shell.sensors function like this:

shell:
  sensors:

     # DHT11 Sensor reporting the temp °F result
    - name: Garage Temperature
      command: 'python3.7 /home/pi/room-assistant/script/myDHT.py'
      regex: '(-?[0-9.]+)F'
      cron: '*/2 * * * *'
      icon: mdi:temperature-fahrenheit
      unitOfMeasurement: '°F'
      deviceClass: temperature

cpuUp.sh

return the uptime in seconds formatted as int

#!/bin/bash

echo `cat /proc/uptime |awk '{print $1}' |cut -d '.' -f1`

cpuTemp.sh

return the CPU reported temp

#!/bin/bash

cpuTemp=`vcgencmd measure_temp |cut -d "=" -f2 |cut -d "'" -f1 | awk '{print ($1 *9/5) + 32}'`
echo $cpuTemp


freeMem.sh

returns the remaining RAM on the pi

#!/bin/bash

memfree=`cat /proc/meminfo | grep MemFree | awk '{print $2}'`; 
memtotal=`cat /proc/meminfo | grep MemTotal | awk '{print $2}'`; 


awk '/MemFree/{free=$2} /MemTotal/{total=$2} END{print (free*100)/total}' /proc/meminfo

#echo $(($memfree * 100 / $memtotal))

myDHT.py

adapted DHT11 temp sensor reading temp and humid

import Adafruit_DHT
import time

DHT_SENSOR = Adafruit_DHT.DHT11
DHT_PIN = 4

while True:
    humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
    if humidity is not None and temperature is not None:
        temperature_f = temperature * (9 / 5) + 32
        print("Temp={0:0.1f}F Humidity={1:0.1f}%".format(temperature_f, humidity))
        exit()
    else:
        time.sleep(.05);

temperature_sensor_code.py

read DS18B20 sensor, report value in F


# Temp DS180b20 reading into F result on raspi
#
# https://pimylifeup.com/raspberry-pi-temperature-sensor/
# Modified from
#   - git clone https://github.com/pimylifeup/temperature_sensor.git

import os
import glob
import time

os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')

base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'

def read_temp_raw():
    lines = []
    i = 0
    # try to read the file 20 times, return the result
    while not lines and (i < 19):
        try:
            f = open(device_file, 'r')
            lines = f.readlines()
            i += 1
        except NameError:
            time.sleep(1)
        else:
            if not lines:
                f.close()
            else:
                f.close()
                return lines

def read_temp():
    lines = read_temp_raw()
    if lines[0].strip()[-3:] != 'YES':
        lines = read_temp_raw()
    equals_pos = lines[1].find('t=')
    if equals_pos != -1:
        temp_string = lines[1][equals_pos+2:]
        temp_c = float(temp_string) / 1000.0
        temp_f = temp_c * 9.0 / 5.0 + 32.0
        return temp_f

# print to reduced decimal place
print('{0:06.3f}'.format(read_temp()))



cpuVolt.sh

return boolean if no issues with voltage (power supply failure)

#!/bin/bash

THROTTLED=`/opt/vc/bin/vcgencmd get_throttled |cut -d "=" -f2`
RESULTS=0

if [[ "$THROTTLED" != *"0x0"*  ]];then
	RESULTS=1
fi

echo $RESULTS


Future Improvements

  • Additional temp sensors affixed to the water pipe lines (hot water usage?)
  • Additional sensor in the crawl space generally (freeze protection).
  • Leak detection around water pipe service connection and sprinkler branch