I love Star Trek (any good SciFi really) and who doesn’t? I also have an Amazon echo and I have been making my home smarter and adding automation where I can, very SciFi’ish yeah? The other day I thought to myself how cool would it be if I could activate a red alert”? Well I do have the Dot and I do have Philips Hue bulbs. I do have a home server and I do have Raspberry Pies. So I decided I wanted to have this feature and set out to get it done. As it turns out I am not the only one that wanted to be able to do this. After Googling I found a few other people that went through a similar process. Although I have not seen anyone do it the way I did. The ones I found mostly accomplished it with a Google Home Assistant and some used Node-Red. Hey, I have Node-Red. I guess the Home Assistant can play audio files. I have found a few pages on Alexa doing this recently but I have not gotten into making skills, yet. All the pages I saw also seemed to rely on an outside service of some kind (minus the voice assistant). They pulled the audio from the web or used IFTTT (which I hate) to do something. I don’t want that. I like to be as self contained as possible. Here’s what I did.
I have my home server setup with NR and it takes it all the MQTT in the house and does all the NR handling for the house. Then I have a Raspberry Pi (that also sits on top of the server), this has a temperature sensor on it and it handles the audio portion of the red alert. Since it is sitting there I also have it monitor the server, and the server monitor the Pi.
Yes I have a server and a Pi, yes they both also run NR. Why do I not use just the server instead of the Pi? The server doesn’t have a sound card and I don’t have an extra one. So yeah.
This flow is where I make the color changes. I use the Wemo Emulator node to create a device Alexa can discover, that also allows me to choose my own trigger word. That node gives a 1 or a 0 (on or off). I pass that to a function that contains the hue bulb color and activates the alert pulsing, this all flows to the hue bulb and out via MQTT. In the flow I only have one hue bulb connected. I have since connected the red alert to all the bulbs I have (currently 4 colored ones).
Here is where I activate the audio. I initially tried omxplayer. I found a shell script that looped the audio but it gave me issues when trying to kill the process. It only worked the first time. The processes didn’t die completely. I want to be able to stop the red alert also. So I continued searching. I came across a post in the NR Google groups and they had created a flow for playing a sound off of motion detection. I was able to take the kill (killall, duh) command he used to stop my flow. I also used the player (mpg123) he used because it has looping options builtin. Sweetness.
I am now able to tell Alexa to turn on a red alert and have her stop it as well. This doesn’t used IFTTT or rely on any other outside source (besides Alexa). It does not require an Alexa skill either. I have future plans to make a custom skill for this so I can change the phrase from “turn on red alert” to something more comfortable like “activate red alert”.
Onward to the flows!
- I am using Node-Red 0.16.2 with Node 6.10.2 with NPM 3.10.10. The server is Ubuntu 16.04.2 LTS and the RPi is Raspbian 8 (Jessie)
- Wemo Emulation Node (http://flows.nodered.org/node/node-red-contrib-wemo-emulator)
- MQTT and an MQTT server (I use Mosquitto but there’s also this Node http://flows.nodered.org/node/node-red-contrib-mqtt-broker)
- Hue Plus Node (http://flows.nodered.org/node/node-red-contrib-hueplus)
- Exec Node (I think it comes with Node-Red?)
- I am running these flows split on two different Node-Red installs (one on the server one on the Pi) but this is not necessary. You could do it all on a Pi or all on a server/computer (with a sound card).
- Don’t forget to turn up the volume on the 3.5mm audio jack amixer set PCM -- -21.00 worked for me.
Flow for the red alert lights:
1 |
[{"id":"6a975dd3.c585c4","type":"inject","z":"9278df22.90e9f","name":"","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"x":390,"y":1880,"wires":[["96966200.a87e4"]]},{"id":"96966200.a87e4","type":"template","z":"9278df22.90e9f","name":"Red Alert","field":"payload","fieldType":"msg","format":"json","syntax":"mustache","template":"{\n \"on\": true,\n \"bri\": 254,\n \"hue\": 0,\n \"sat\": 254,\n \"longAlert\": true\n}","x":600,"y":1880,"wires":[["51c717fc.c355f8"]]},{"id":"59b047d4.862c68","type":"wemo-emulator","z":"9278df22.90e9f","name":"Red Alert","friendlyName":"Red Alert","serial":"10002","port":"39002","onTopic":"redalert/","onPayload":"1","offTopic":"redalert/","offPayload":"0","x":400,"y":1960,"wires":[["2034e881.172828","748be90.58e4318"]]},{"id":"748be90.58e4318","type":"switch","z":"9278df22.90e9f","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"str"},{"t":"eq","v":"0","vt":"str"}],"checkall":"true","outputs":2,"x":590,"y":2000,"wires":[["96966200.a87e4","5e0d52a6.3c239c"],["5e0d52a6.3c239c"]]},{"id":"5e0d52a6.3c239c","type":"mqtt out","z":"9278df22.90e9f","name":"","topic":"redalert/","qos":"","retain":"","broker":"570eff6c.16b94","x":780,"y":2000,"wires":[]},{"id":"2034e881.172828","type":"debug","z":"9278df22.90e9f","name":"","active":false,"console":"false","complete":"false","x":810,"y":1960,"wires":[]},{"id":"51c717fc.c355f8","type":"hue-set","z":"9278df22.90e9f","bridge":"1ec2a86e.adc7b8","light":"2","name":"Family Room","x":790,"y":1880,"wires":[["cafba74a.d6fce8"]]},{"id":"cafba74a.d6fce8","type":"debug","z":"9278df22.90e9f","name":"","active":true,"console":"false","complete":"false","x":810,"y":1920,"wires":[]},{"id":"570eff6c.16b94","type":"mqtt-broker","z":"","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""},{"id":"1ec2a86e.adc7b8","type":"hue-bridge","z":0,"name":"","bridge":"10.0.0.230","key":"jjBeD5yGu63N9wfHLgcbNTYG2NApHzwckPQrr47o"}] |
Flow for the red alert audio:
1 |
[{"id":"49ba982e.4b9a78","type":"mqtt in","z":"2f02033f.39628c","name":"","topic":"redalert/","qos":"2","broker":"9fc72cf8.8cf19","x":180,"y":180,"wires":[["30803d1c.942dd2","e88c1df9.09f09"]]},{"id":"191dbbbd.6b4e94","type":"exec","z":"2f02033f.39628c","command":"mpg123 --loop 3 /home/pi/red_alert.mp3","addpay":true,"append":"","useSpawn":"","timer":"","name":"","x":600,"y":140,"wires":[["1b2f0b04.f67f15"],["1b2f0b04.f67f15"],["1b2f0b04.f67f15"]]},{"id":"1b2f0b04.f67f15","type":"debug","z":"2f02033f.39628c","name":"","active":true,"console":"false","complete":"false","x":890,"y":140,"wires":[]},{"id":"ec620bb8.6e0a58","type":"inject","z":"2f02033f.39628c","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":180,"y":140,"wires":[["191dbbbd.6b4e94"]]},{"id":"e88c1df9.09f09","type":"debug","z":"2f02033f.39628c","name":"","active":true,"console":"false","complete":"false","x":330,"y":220,"wires":[]},{"id":"30803d1c.942dd2","type":"switch","z":"2f02033f.39628c","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"str"},{"t":"eq","v":"0","vt":"str"}],"checkall":"true","outputs":2,"x":330,"y":180,"wires":[["191dbbbd.6b4e94"],["6107f1b1.f801a"]]},{"id":"6107f1b1.f801a","type":"exec","z":"2f02033f.39628c","command":"killall mpg123","addpay":true,"append":"","useSpawn":false,"timer":"","name":"","x":520,"y":200,"wires":[["b1bea1e0.f1441"],["b1bea1e0.f1441"],["b1bea1e0.f1441"]]},{"id":"b1bea1e0.f1441","type":"debug","z":"2f02033f.39628c","name":"","active":true,"console":"false","complete":"false","x":890,"y":200,"wires":[]},{"id":"3d067ced.a36f34","type":"inject","z":"2f02033f.39628c","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":180,"y":260,"wires":[["6107f1b1.f801a"]]},{"id":"9fc72cf8.8cf19","type":"mqtt-broker","z":"","broker":"10.0.0.10","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""}] |
(You may notice that the flows are different from the images, I cleaned up the flow before exporting and pasting the code.)
UPDATE 8/29/2017: I added updated code/flows so that you can disable the Red Alert lights. Previously a “stop node red” command would silence the sounds but not cancel the flashing lights. With the new update the sounds stop and the lights stop flashing and turn white. Still working on getting the lights to default to white after the Red Alert times out.
UPDATED RED ALERT STOP FLOW 8/29/2017
1 |
[{"id":"90f3c2dc.e9141","type":"mqtt in","z":"550fa05c.dd984","name":"","topic":"redalert/","qos":"2","broker":"2d8b1766.cc6c78","x":262.5,"y":540,"wires":[["a016e9e4.6d6bd8","2ecf8008.876e7"]]},{"id":"5299e099.10b9","type":"exec","z":"550fa05c.dd984","command":"mpg123 --loop 3 /home/pi/red_alert.mp3","addpay":true,"append":"","useSpawn":"","timer":"","name":"","x":682.5,"y":500,"wires":[["76982adc.eec474"],["76982adc.eec474"],["76982adc.eec474"]]},{"id":"76982adc.eec474","type":"debug","z":"550fa05c.dd984","name":"","active":true,"console":"false","complete":"false","x":972.5,"y":500,"wires":[]},{"id":"995caace.047ff8","type":"inject","z":"550fa05c.dd984","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":262.5,"y":500,"wires":[["5299e099.10b9"]]},{"id":"2ecf8008.876e7","type":"debug","z":"550fa05c.dd984","name":"","active":true,"console":"false","complete":"false","x":412.5,"y":580,"wires":[]},{"id":"a016e9e4.6d6bd8","type":"switch","z":"550fa05c.dd984","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"str"},{"t":"eq","v":"0","vt":"str"}],"checkall":"true","outputs":2,"x":412.5,"y":540,"wires":[["5299e099.10b9"],["c5cebc4a.16a9e","33592eb9.80ded2"]]},{"id":"c5cebc4a.16a9e","type":"exec","z":"550fa05c.dd984","command":"killall mpg123","addpay":true,"append":"","useSpawn":false,"timer":"","name":"","x":602.5,"y":560,"wires":[["a20f50fd.a9675"],["a20f50fd.a9675"],["a20f50fd.a9675"]]},{"id":"a20f50fd.a9675","type":"debug","z":"550fa05c.dd984","name":"","active":false,"console":"false","complete":"false","x":972.5,"y":560,"wires":[]},{"id":"1a90c181.1b188e","type":"inject","z":"550fa05c.dd984","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":260,"y":620,"wires":[["c5cebc4a.16a9e"]]},{"id":"33592eb9.80ded2","type":"template","z":"550fa05c.dd984","name":"Red Alert Off","field":"payload","fieldType":"msg","format":"json","syntax":"mustache","template":"{\n \"on\": true,\n \"bri\": 254,\n \"hue\": 0,\n \"sat\": 0,\n \"alert\": \"none\"\n}","x":530,"y":700,"wires":[["f76585d7.49d2b8","7528f89f.558948","3f3fea74.48c446"]]},{"id":"f76585d7.49d2b8","type":"hue-set","z":"550fa05c.dd984","bridge":"f908d659.801f38","light":"2","name":"Family Room","x":770,"y":720,"wires":[["6c88fdae.4505f4"]]},{"id":"7528f89f.558948","type":"hue-set","z":"550fa05c.dd984","bridge":"f908d659.801f38","light":"g-6","name":"Entry Way","x":780,"y":680,"wires":[["6bca4180.a8e3"]]},{"id":"3f3fea74.48c446","type":"hue-set","z":"550fa05c.dd984","bridge":"f908d659.801f38","light":"3","name":"Computer Desk","x":760,"y":640,"wires":[["f6331dcd.4fe9a"]]},{"id":"6c88fdae.4505f4","type":"debug","z":"550fa05c.dd984","name":"","active":true,"console":"false","complete":"false","x":970,"y":720,"wires":[]},{"id":"6bca4180.a8e3","type":"debug","z":"550fa05c.dd984","name":"","active":true,"console":"false","complete":"false","x":970,"y":680,"wires":[]},{"id":"f6331dcd.4fe9a","type":"debug","z":"550fa05c.dd984","name":"","active":true,"console":"false","complete":"false","x":970,"y":640,"wires":[]},{"id":"8d68ebed.86b658","type":"inject","z":"550fa05c.dd984","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":300,"y":700,"wires":[["33592eb9.80ded2"]]},{"id":"2d8b1766.cc6c78","type":"mqtt-broker","z":"","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""},{"id":"f908d659.801f38","type":"hue-bridge","z":0,"name":"","bridge":"10.0.0.230","key":"jjBeD5yGu63N9wfHLgcbNTYG2NApHzwckPQrr47o"}] |
Some pages I found helpful.
Where I got the red alert sound
http://trekcore.com/audio/
Play Ambient audio on motion detection
https://groups.google.com/forum/#!searchin/node-red/audio|sort:relevance/node-red/vwQq8Plk0Zg/6DV5ZYMRCAAJ
The code for the ambient audio
https://github.com/natcl/exporail_video_player
Google Home Assitant and an RPi with video
https://www.youtube.com/watch?v=7j3QQlc_efY