INSTAR Deutschland GmbH

Node-RED and Alexa



A Node-RED node to control things via Amazon Alexa. Use voice commands to control your INSTAR IP camera without the need for an Amazon Alexa Skill - no Cloud required. We are going to use the node-red-contrib-alexa-local to create an endpoint for Alexa to control. It was designed to emulate a Philips Hue bridge & device within local network. Amazon Echo is hardcoded to support only on/off/dimming command via this route. Any other type of support has to go through the Alexa Skills route (cloud-based).

Be aware:

At the moment of writing only the Alexa Echo and Alexa Echo Dot Gen 1 & 2 are supported. The Node doesn't support Echo Gen 2, Echo Dot Gen 3 & Echo Plus local devices discovery yet and Echo Show, Echo Spot, Sonos One do not have the capability to discovery devices locally.

A Echo Dot Gen 2 was used for this tutorial.

Node-RED

We already installed Node-RED under Windows and on a Raspberry Pi. Open the admin panel and go to the Manage Palette menu to install the alexa-local node:


Node-RED and Alexa

Node-RED and Alexa

You should see a new Input Node called alexa local. Drag it onto the working area and double-click it. Type in a function name - e.g. Area One. This is the name that Alexa will use to identify the device. And the name you have to use to select it - so choose its name wisely.


Node-RED and Alexa

Node-RED and Alexa

Connect a Debug Node to its output and Deploy the flow. Then ask "Alexa, discover devices" - Alexa should reply that it discovered Area One.

You can test it by issuing the following commands:

  • "Alexa, turn off Area One"
  • "Alexa, switch on Area One"

Node-RED and Alexa

You will see that the corresponding payloads - on or off are received by the Alexa Local Node. To be able to send percentage values, we will have to add a little Function Node to our flow. It needs to be connected to the output of the Alexa Local Node and listens for a Brightness value to be send with the Alexa command. Remember that we are exploiting a function here that is meant to be used by local light switches on your network?


Node-RED and Alexa

The function leaves the payload intact - if the incoming command is of the ON/OFF nature. But if a brightness value is send, it will set this to become the payload of the message:

var a = {payload :msg.bri };
var b = { payload:msg.on_off_command };
var c = { payload: msg.payload };
 

if ( msg.on_off_command === true){
 return c;
} else {
 return a ;
}

You can now use the following example commands:

  • "Alexa, set Red Area Sensitivity to 40%"
  • "Alexa, set Red Area Sensitivity to 30"
  • "Alexa, increase Red Area Sensitivity"
  • "Alexa, decrease Red Area Sensitivity"

Node-RED and Alexa

You will see the % values transmitted in the Debug Node.

Add the MQTT input to an existing Flow

In the following sequence we are using the input from Alexa to switch Alarm Areas on our cameras - we already used the same sequence for the Zigbee Integration (the complete flow can be found in the Home Assistant Tutorial):


Node-RED and Alexa

You can copy the following JSON code and import it into Node-RED (How do I import flows to Node-RED?):

[{"id":"2b897ea7.5d2e42","type":"alexa-local","z":"17b43655.05bd3a","devicename":"Red Test","inputtrigger":false,"x":760,"y":160,"wires":[["150d2a21.1cd336"]]},{"id":"150d2a21.1cd336","type":"function","z":"17b43655.05bd3a","name":"","func":"var a = {payload :msg.bri };\nvar b = { payload:msg.on_off_command };\nvar c = { payload: msg.payload };\n \n\nif ( msg.on_off_command === true){\n return c;\n} else {\n return a ;\n}","outputs":1,"noerr":0,"x":890,"y":160,"wires":[["69e3e027.1310a"]]},{"id":"c6ea8672.c28078","type":"link out","z":"17b43655.05bd3a","name":"","links":["57b22bf7.0e2874","5eb0d369.b3b15c","b5ce97db.b3f4f8","f335d7f4.4bbe18"],"x":1115,"y":160,"wires":[]},{"id":"69e3e027.1310a","type":"string","z":"17b43655.05bd3a","name":"","methods":[{"name":"replaceAll","params":[{"type":"str","value":"on"},{"type":"str","value":"{\"val\" : \"on\"}"}]},{"name":"replaceAll","params":[{"type":"str","value":"off"},{"type":"str","value":"{\"val\" : \"off\"}"}]}],"prop":"payload","propout":"payload","object":"msg","objectout":"msg","x":1033,"y":160,"wires":[["c6ea8672.c28078"]]}]

All we needed to add is a String Node (this node type has to be installed manually - it is called node-red-contrib-string) that searches for the payload on or off and replaces them with something that we need to trigger the Flow below - in this case we need a JSON expression that either sets a value {"val" : "on"} or {"val" : "off"}:


Node-RED and Alexa

We then add a Link Node to plug it in to all 4 sequences for the for the 4 alarm areas and Deploy. Now we can activate all areas with the command: Alexa, activate Red Test. And deactivate them by saying Alexa, switch off Red Test:


Node-RED and Alexa