如何在Node-RED中通过比较控制的开关节点发送指定HTTP请求控制Pokeys57e
Hey Brendan, here's a straightforward, no-fuss way to swap out those limited pre-built nodes and get your relay control working exactly like the browser URL you tested:
1. Keep Your Existing Comparison Logic
First, hold onto the node you’re using for the greater/less than check (whether that’s a Switch node or a custom Function node). We just need to redirect what happens after the condition is met—no need to rebuild your core logic.
2. Add an HTTP Request Node
Drag an HTTP Request node from the Node-RED palette into your flow. Configure it with these settings:
- Method: Select
GET(matches how your browser sends the request) - URL: Leave this blank for now (we’ll set it dynamically based on your condition)
- Return: Pick
a UTF-8 string(you don’t need the response content, but this is a safe default)
3. Dynamically Set the Request URL
You’ll need to toggle the State parameter (1 for on, 0 for off) based on your comparison result. Here are two simple ways to do this:
Option A: Switch + Change Nodes (No Code Needed)
- Connect your comparison node to a Switch node. Split the flow into two branches: one for when your condition is true (e.g., payload > 50) and one for when it’s false.
- Add a
Changenode to each branch:- For the "true" branch: Set
msg.urltohttp://192.168.0.2/setS.html?Sensor=00&State=1&u=user&p=pass - For the "false" branch: Set
msg.urltohttp://192.168.0.2/setS.html?Sensor=00&State=0&u=user&p=pass
- For the "true" branch: Set
- Link both Change nodes to your HTTP Request node.
Option B: Function Node (For More Flexibility)
If you prefer a bit of code for tighter control, replace the Switch/Change setup with a single Function node:
// Replace "yourThreshold" with the actual number you’re comparing against if (msg.payload > yourThreshold) { msg.url = "http://192.168.0.2/setS.html?Sensor=00&State=1&u=user&p=pass"; } else { msg.url = "http://192.168.0.2/setS.html?Sensor=00&State=0&u=user&p=pass"; } return msg;
- Connect your comparison node to this Function node, then link the Function node to the HTTP Request node.
4. Test and Deploy
Hit deploy, then trigger the condition that should activate the relay. You can add a Debug node after the HTTP Request node to verify the request is sending correctly—but since your browser URL works, this should function exactly as expected.
Bonus: Optional Error Handling
If you want to catch cases where the Pokeys57e is offline or unresponsive, connect a Catch node to the HTTP Request node to log errors or trigger alerts.
内容的提问来源于stack exchange,提问作者Brendan Kirk

