D4E1: Goodmorning From Google Home / Nest With Motion Dectection
by FrederikDC in Circuits > Gadgets
816 Views, 12 Favorites, 0 Comments
D4E1: Goodmorning From Google Home / Nest With Motion Dectection

This project automatically triggers a custom routine on a Google Home / Nest device when someone enters the room. This is detected using an Ikea motion sensor.
Schematically, the project looks like this:
- The motion sensors connects to a Raspberry Pi using a Zigbee dongle.
- The Raspberry Pi runs Zigbee2MQTT and Node-RED
- A chromecast plays two MP3 files with commands to trigger the the Google Home / Nest. This additional step is required because a Google Home / Nest always requires a voice command to get triggered.
- The Google Home / Nest searches for the custom routine and plays the results.
Little to no code was written to realise this project because Node-RED is a no-code / low-code environment.
Supplies

The following materials were used during this project:
- A Raspberry Pi model 3 B+ including
- A power adapter
- A 16GB SD card for storage (you can also use a 8GB card)
- A Sonoff Zigbee 3.0 Dongle
- An IKEA wireless motion sensor
- A Google Home or Nest
- A Chromecast or other device that can play mp3 files
Creating a New Routine for Your Google Home / Nest

The first step is creating a custom routine for your Google Assistent. This allows you to execute multiple actions on the Google Home / Nest with a single voice command. Check the manual for Google Nest for a step-by-step guide on how to create a custom routine.
You can simply test the routine by using the starter command on you Google Home / Nest device. Test the routine first before continuing to the next steps.
Record Voice Command

The Google Home / Nest requires a voice command (or a fixed time) to start a routine . This voice command is given by a Chromecast. However, you can use any device that can play MP3 files.
NOTE: I first tried Text-to-speech on a Chromecast to give the Google Home / Nest a starting command, but it was not always recognised by the Google Home / Nest. This is the reason why I decided to record the commands with my own voice.
- Go to https://online-voice-recorder.com/
- Click the red microphone and allow your browser to use your microphone
- Record "Ok, Google" and save it as ok.mp3
- Record: "Start my day" and save it as start.mp3
The files will be moved to the Raspberry Pi in Step 4.
Installing Node-red on Raspberry Pi

Install Raspberry Pi OS Lite
First you need to install an operating system on the Raspberry Pi. The Lite version will do for this project.
- Download the Raspberry Pi Imager tool and install it.
- Remove the SD Card from the Pi and put it in your laptop
- Launch the Raspberry Pi Imager
- Click CHOOSE OS
- Choose Raspberry Pi OS Lite (32 bit) as we don't need a full Desktop version.
- Click CHOOSE STORAGE and choose your SD Card
- Click on the gear icon for the following settings:
- Give your Pi a name. For example: homeserver.local
- Make sure SSH access is enabled. This way you don't need an external screen or keyboard for further configuration
- Set a username (for example: pi) and a password (for example: apple).
- Set your WiFi settings
- Optionally set the region settings
Finally, click WRITE and choose Yes at the prompt that all files will be deleted. After a few minutes, the OS is installed and the SD Card can be reinserted into the Pi.
Boot the Pi by plugging in the power adapter. After a few seconds it will already be booted as the lite version of the operating system has been installed.
Install Node-RED
Node-RED is a visual programming language that allows you to develop a complete application by linking different blocks (nodes) together. All installation steps can be found in the Node-RED documentation.
Connect to your Pi using SSH and login with your credentials (password is in my case apple)
ssh pi@homeserver.local
Enter this command and confirm with y. Answer N on the question to install Pi-specific libraries since we do not need them for this project.
bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)
Wait for a couple of minutes until the installation completes. You can check the log file for error if you want to.
Make sure that Node-Red will start immediately after the Pi has been booted by entering
sudo systemctl enable nodered.service
Finally reboot the Pi
sudo reboot
Test if the installation was successful by opening your browser and navigate to http://homeserver.local:1880/. If you see the Node-Red welcome screen, you are good to go. Otherwise you might need to check for the IP address of your Pi and use http://[ip-of-you-pi]:1880 instead.
Getting the Raspberry Pi Ready

You need to install a couple of tools on the pi to make it a zigbee gateway. First update Raspberian OS to the latest version.
sudo apt update
Install Git
Git is a command line tool used by software developers for version control of their source code. It is also used to exchange code and projects. We use it to clone the zigbee2mqtt project.
Enter the following command and pres Y
sudo apt install git
Check if the installation succeeded with the following command. The installation is successful if it does not return an error.
git --version
Install Mosquitto
MQTT is the standard for sending messages in an IoT environment. It is a simple protocol that uses a publish / subscribe architecture. MQTT uses a broker to guide messages from publisher to subscriber. Be sure to check https://mqtt.org/ for more information about the MQTT protocol. This project uses Mosquitto as a broker.
Enter the following command to install it.
sudo apt install -y mosquitto mosquitto-clients
Enter the following command to start Mosquitto every time the Pi boots
sudo systemctl enable mosquitto.service
Check if the installation succeeded with the following command. It might return an error that the service is already running, but this is not a problem.
mosquitto -v
Install Nginx
Nginx is a lightweight webserver. We need this webserver to host the mp3 files that will trigger the Google Home / Nest because a Chromecast can only play files that are hosted over http.
Enter the following command to install it and pres Y
sudo apt install nginx
Copy files to the Pi
The final step is to copy the mp3 files from your local machine to the Pi. Open the terminal on your local machine and navigate to the directory that hosts the mp3 files. Enter the following command
scp ok.mp3 start.mp3 pi@homeserver.local:/home/pi
This copies the files from your local machine to your pi. Now move them to the root folder of your webserver which is var/www/html
Login to the pi using ssh and enter the following command
sudo mv ok.mp3 start.mp3 /var/www/html
Test if the files are copied by opening a browser window on your local machine and navigate to http://homeserver.local/ok.mp3. This should show a media player in your browser
Installing Zigbee2MQTT

The steps in this section are based on the installation guide of Zigbee2MQTT for Linux. Shut down your Raspberry Pi, insert the dongle and reboot the Pi before continuing.
Installation
After reboot, login using ssh and check the serial port where the dongle is connected
ls -l /dev/serial/by-id
This command returns a list of connected USB devices. The name in my case is ttyUSB0. This name will be needed during configuration.
Clone the zigbee2mqtt project
git clone https://github.com/Koenkk/zigbee2mqtt.git
Move it to a new directory
sudo mv zigbee2mqtt /opt/zigbee2mqtt
Navigate to that directory
cd /opt/zigbee2mqtt
And start the installation
npm ci
Configuration
Open the configuration file with the following command
nano /opt/zigbee2mqtt/data/configuration.yaml
Adjust the file in 3 places:
- add frontend: true at the top
- add the advanced section at the bottom
- change the port to ttyUSB0 or the name that was returned during installation.
The final result looks like this. Press Ctrl + X followed by Y and Enter to save the configuration file.
frontend: true # MQTT settings mqtt: # MQTT base topic for Zigbee2MQTT MQTT messages base_topic: zigbee2mqtt # MQTT server URL server: 'mqtt://localhost' # MQTT server authentication, uncomment if required: # user: my_user # password: my_password # Serial settings serial: # Location of the adapter (see first step of this guide) port: /dev/ttyUSB0 advanced: network_key: GENERATE
Starting the first time
Navigate to the folder that holds the installation
cd /opt/zigbee2mqtt
Start zigbee2mqtt. This will take a while the first time.
npm start
You should see the following output after a while
Zigbee2MQTT:info 2019-11-09T13:04:01: Logging to directory: '/opt/zigbee2mqtt/data/log/2019-11-09.14-04-01' Zigbee2MQTT:info 2019-11-09T13:04:01: Starting Zigbee2MQTT version 1.6.0 (commit #720e393) Zigbee2MQTT:info 2019-11-09T13:04:01: Starting zigbee-herdsman... Zigbee2MQTT:info 2019-11-09T13:04:03: zigbee-herdsman started Zigbee2MQTT:info 2019-11-09T13:04:03: Coordinator firmware version: '{"type":"zStack30x","meta":{"transportrev":2,"product":2,"majorrel":2,"minorrel":7,"maintrel":2,"revision":20190425}}' Zigbee2MQTT:info 2019-11-09T13:04:03: Currently 0 devices are joined: Zigbee2MQTT:warn 2019-11-09T13:04:03: `permit_join` set to `true` in configuration.yaml. Zigbee2MQTT:warn 2019-11-09T13:04:03: Allowing new devices to join. Zigbee2MQTT:warn 2019-11-09T13:04:03: Set `permit_join` to `false` once you joined all devices. Zigbee2MQTT:info 2019-11-09T13:04:03: Zigbee: allowing new devices to join. Zigbee2MQTT:info 2019-11-09T13:04:03: Connecting to MQTT server at mqtt://localhost Zigbee2MQTT:info 2019-11-09T13:04:03: Connected to MQTT server
Starting automatically
Finally configure zigbee2mqtt to start every time the Pi boots. Create a new config file with the following command:
sudo nano /etc/systemd/system/zigbee2mqtt.service
Enter this content
[Unit] Description=zigbee2mqtt After=network.target [Service] ExecStart=/usr/bin/npm start WorkingDirectory=/opt/zigbee2mqtt StandardOutput=inherit # Or use StandardOutput=null if you don't want Zigbee2MQTT messages filling syslog, for more options see systemd.exec(5) StandardError=inherit Restart=always RestartSec=10s User=pi [Install] WantedBy=multi-user.target
Press Ctrl + X followed by Y and Enter to save the configuration file.
Enable the service:
sudo systemctl enable zigbee2mqtt.service
Reboot the Pi.
Connecting a Zigbee Device

Your Raspberry Pi is now converted into a Zigbee gateway and can therefore communicate with any device that uses the Zigbee protocol without the requirement of a vender gateway. An overview of all the supported devices can be found on https://www.zigbee2mqtt.io/supported-devices/.
We will connect a motion sensor of IKEA. Check the documentation te see how you can pair the sensor wand what data is exposed.
To connect the sensor:
- Navigate to http://homeserver.local:8080. You see the dashboard of zigbee2mqtt
- press the link button on the back of the sensor 4 times in a row
- The dashboard shows a message that it tries to connect to a new device. It is possible that you need to retry a couple of times before it is connected
- Click on newly added device and offer it a more readable name. For example: motion_sensor
If you click on the tab logs, you can see that the sensor sends messages in JSON format to the gateway. The following step will create an application that captures these messages.
"Writing" the Code in Node-RED

Navigate to http://homeserver.local:1880 to open the Node-RED editor and take the following steps to reproduce code:
- Open the menu --> Manage pallete and install the node node-red-contrib-castv2. This is required to connect with Google devices in the network.
- Open the menu --> Import and copy / paste the code in json format
- Double click node Chromecast office and click the pencil next to Zolder or Add new castv2-connection...
- Click the magnifier icon to detect your Google devices
- Select the desired Chromecast
- Click the Deploy button in the upper right
A short explanation on the code:
- zigbee2mqtt/motion_sensor. This is a MQTT-out node that subscribes to the messages published by the zigbee gateway. Make sure that motion_sensor is the name of your sensor or adjust the name in this node
- check detected. The sensor returns a message each time it detects movement and when it goes back to detecting new movements (by default 90 seconds). This node filters the messages and passes only the ones where a new detection was found.
- trigger 3s. This node passes the url of the mp3 files to the chromecast with a delay of 3 seconds. This is to offer the Google Home / Nest the time to activate itself after the command "ok google".
- manual input. This node is used to manually inject a detection for testing purposes.
- Chromecast office. This node represents the Chromecast and does not require further configuration.
Testing

Test your application. You can first test using the manual input node. If this works, you can use the motion sensor. Check the debug panel for potential errors and add debug nodes if necessary.