node-RED making HTTP requests when starting up

I needed to make use of the HTTP endpoint of the switches because the MQTT functionality of the switches did behave really weird and after hours of checking the configs and digging in the code, I had given up.

So I created a workaround: The MQTT messages are being sent to my node-RED instance which upon receiving a MQTT message of a certain topic, triggers the HTTP request which switches the Sonoff either off, on or toggles it’s state.

Ever since, I experienced a weird problem:
When I restart my node-RED docker container, my Sonoff Switches are being toggled very quickly.
It seems like either all or the “toggle” HTTP Requests are being sent when node-RED loads the flows. Any ideas how I can turn this off?

My flows.json is here:

Thank you in advance :slight_smile:

Take a look on this:
https://flows.nodered.org/flow/a886a507c57ea83a3a5260338091505b

or

Are you sure the problem was not just that you had the Message Pass Through checkbox checked on the switch?

@Colin: You mean in the Sonoff configuration?

I don’t understand how this can fix my problem. I would expect node-RED not to send any on/off or toggle command when starting. The only commands that should be send are the commands to retrieve the status of a switch

You can save the state of the switch or any other value as a global variable and recover the value at deploy/restart take a look on the docu, I think it will help u.

From other side I have sonoff modules with tasmota and does not have problem at all using MQTT… maybe if you give more info, I thought you wanted to recover previous state but see quetion has noting to do with it, In you case I think MQTT is working very reliable, paste your flow…

Regards

I assumed you were talking about dashboard switches driving Sonoff ones. I see now that is not what you meant.

You can find my flow in the first post of this thread.

I managed to get the MQTT working now… I must have been very stupid before. But I still have one problem: The Sonoff sends it’s state every 10 seconds which consumes too much WIFI airtime in my opinion. I didn’t find an option to change it.

So at the moment, I still want to turn off the MQTT feature on the Sonoffs and find out why node-RED flips the switches when I starts (it also happens if I change a flow and click “Deploy -> Full”)

I asume you are working with tasmota on the sonoff, check config inside "configure logging"

image

One easy option to make what you wanted is to do something like this....

Inject after 5 seconds start only to write a variable, then set before http node out a switch that filter and does not allow to have any flow moving until 5 seconds (as instance) after boot, this probably will solve your issue however I don't know what is exactly happening to you to have this effect.

Something like:

image

I'm using this to filter noise as startup and avoid to have some messages logged.

Regards

Yes, I use tasmoat. The telemetry period value is measured in seconds, right?

My switch also has the default value of 300 but sends it’s state every 10 seconds.

I will try the flow you have shown me.

I hope you are not making a loop or something like this....

Check that you are sending the command (per default) to " cmnd/unit_name/POWER" and receive the feedback from "stat/unit_name/POWER"

Something else to check, on sonoff, have you set the power to be retained to avoid change when MQTT connection to server is lost?

PowerRetain is set to off

Your solution with the global variable seems to work :slight_smile:

Thanks a lot

But still on “Deploy -> Full” the switches are flipped because then, the variable is already set. Is there a way to clear the variable on “Deploy” ?

Hello,

I have made some trick in some case that maybe helps you…

I make a inject at start after 3 seconds to write a global variable called for instance “restart” to off.

Then I put a switch that checks global.restart = off or flow is interrupted.

What I make with this is to block any message until 3 seconds after restart/deploy.

Maybe u can try…

something like:

[{"id":"41a37cd3.81c814","type":"inject","z":"c1bcf04c.5b249","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":"3","x":247.77777099609375,"y":3107.777587890625,"wires":[["5064e7d6.9bf9c8"]]},{"id":"5064e7d6.9bf9c8","type":"change","z":"c1bcf04c.5b249","name":"","rules":[{"t":"set","p":"restart","pt":"global","to":"off","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":511.77771759033203,"y":3101.5553245544434,"wires":[[]]},{"id":"eae03de9.d060c","type":"rpi-gpio out","z":"c1bcf04c.5b249","name":"","pin":"","set":"","level":"0","freq":"","out":"out","x":486.66666666666663,"y":3194.4444444444443,"wires":[]},{"id":"5b065d73.466384","type":"switch","z":"c1bcf04c.5b249","name":"","property":"restart","propertyType":"global","rules":[{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":319.9999885559082,"y":3195.555709838867,"wires":[["eae03de9.d060c"]]}]

The solution works. But I have noticed something else:

My MQTT broker is a RabbitMQ with MQTT Plugin. When I restart the RabbitMQ, all the switches are flipped, too.

It seems that this happens when node-RED reconnects to the RabbitMQ.

is that a raspberry? if yes start trying mosquitto works very smooth

Regards

My guess is that not the MQTT broker itself is the problem. I think it’s either the configuration or the behaviour of node-RED when reconnecting to the broker.

If you have set any messages to be retained by the broker - then when you reconnect the client (Node-RED) then it will receive those messages. Depends how your application if you need to retain them or not.