The Beginner’s Guide to Node-red with Home-Assistant (Part 2: Switch Node)

The Beginner's Guide to Node-red with Home-Assistant (Part 2: Switch Node)

In part 1 of this series we discussed simple configuration of the MQTT in and out nodes, how to send a payload of On or Off and how to monitor incoming information in the debug window. In this post we’re going to expand on this knowledge further and use a sensor that’s already in Home-Assistant to trigger a series of outcomes according to it’s payload. I recommend you read part 1 if you’re new to Node-red or just want to refresh your memory.

If you’ve got Node-red and Home-Asssitant linked successfully, you should find that your entities are available in Node-red from drop down boxes when you go to configure inidividual nodes. I have several Xiaomi Aqara zigbee sensors that I use everyday in Home-Assistant via Zigbee2mqtt. I am going to pull the information from one of the motion sensors so that we can trigger an event if motion is detected or not. To pull sensor information out of home-asssitant I am going to use the state_changed node.

state changed node
state changed node

 

I know what my entity id is in home-assistant by clicking on the sensor itself and making note of the full name and then finding it in the dropdown menu in Node-red.

Configuring a motion sensor state changed node
Configuring a motion sensor state changed node

Once configured, attach a debug node and trip the sensor. Below you’ll see the resultant output.

Motion Sensor tripped debug
Motion Sensor tripped debug

After 90 seconds (the default for Xiaomi Aqara) the motion sensor returns to it’s “clear” state and sends a payload of “off”. Now that we know the payloads that are sent, we can play with these to make them do more. Let’s introduce the switch node, or as I like to think of it, the gate.

switch node
switch node

The switch node for me is probably one of the most useful and essential nodes there are in Node-red. It controls what happens according to the output. Couple of things to be aware of. When dealing with the switch node, the payloads must match exactly, everything is case sensitive here. If the motion sensor sends on and off, then that’s what we need to account for. See below for how I’ve configured the switch.

Configuring the swtich node
Configuring the swtich node

Note by default, you’ll only have 1 output. If you look at the bottom, you can click the small add button and add as many outputs as you like. The theory is that if the payload of on is received, then the flow continues along output 1, if off is received then the flow diverts instead to output 2. I want to add some functionality to the flow now and build on what we learnt in part 1. I am going to add the MQTT trigger that we created for turning a light on and off to the outputs of this switch. The result should be if motion is detected, the light turns on, and when clear, the light turns off.

Turning a light on or off according to motion detection
Turning a light on or off according to motion detection

This works. Now because we’re sending the payload along the chain, there’s no need for any additional nodes to modify the message, and you’re able to link both outputs to the same MQTT out node. If I wanted to do different things, I could easily only link one output onwards and ignore the other, or I could connect them to different nodes and flows entirely.

What we have essentially achieved is a 90 second motion detected light. After 90 seconds the motion sensor sends the off payload and the light switches itself off. Now what if 90 seconds is too soon and we want it to last 5 minutes instead. That’s simple enough to achieve, we can simply add a delay node. By default it’s set to 5 seconds, but it’s very easy to change this as you need it.

delay node
delay node

Note I am only adding this to the “off” side of the outputs as I want a delay in switching off, not on switching on.

Motion sensor with delay node
Motion sensor with delay node

Let’s move through another example of the switch node. I won’t fill in all the steps in quite as much detail as you should all be relatively comfortable with the configuration side of things.

Sticking with Xiaomi Aqara, I have a zigbee push button set up in home-assistant. I know it can respond to various types of clicks. Let’s use the debug window to analyse the payload that is sent when we try different types of clicks (single, double, long press etc). Once we have the payloads we’ll use the switch node to choose what to do accordingly. In this example, rather than use MQTT to trigger a tasmota device, I want to control various lights via home-assistant directly.

Aqara push button
Aqara push button
Aqara push button payloads
Aqara push button payloads

Now you can see how powerful the debug node is! Let’s set up a switch accordingly…

Type of Click Switch
Type of Click Switch
type of click flow
type of click flow

As you can see from the above, the top output is output 1 and so on. The service call node I’m using here is the node that integrates directly with your home-assistant entities allowing you to control them via Node-red. Below is how you can configure them. When dealing with switches you have the ability to turn_on, turn_off, and toggle. Note in the bottom example, I show you how to turn_off more than one entity by using the comma. You could just as well have added an additional turn off node to output 3 or even after the existing node on output 3, but this is a little bit neater.

 

Toggle switch example
Toggle switch example
Multiple Entities turned off example
Multiple Entities turned off example

On testing this works as expected.

Summary

So in this post, we’ve learnt how to take sensor information directly from Home-assistant via the state_change node and also how to control entities directly in home-assistant via the service call node. We’ve also covered how to set up the switch node to direct flows according to the payload received, and also how to add delays as appropriate.

If you’re interested in finding out more about the sensors I’m using you can check out my posts here on how to configure them here, and my two year review on them here.

Comments

  1. The “for time” option for “if state is” is not present with . Because? Bug? Thanks

Leave a Reply