# node-RED making HTTP requests when starting up

**URL:** <https://discourse.nodered.org/t/node-red-making-http-requests-when-starting-up/1211>\
**Category:** General\
**Created:** [30 June 2018 20:25 UTC](https://discourse.nodered.org/t/node-red-making-http-requests-when-starting-up/1211 "2018-06-30T20:25:12Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![cdfx](https://avatars.discourse-cdn.com/v4/letter/c/e36b37/32.png) [@cdfx](https://discourse.nodered.org/u/cdfx)\
**Post date:** [30 June 2018 20:25 UTC](https://discourse.nodered.org/t/node-red-making-http-requests-when-starting-up/1211/1 "2018-06-30T20:25:12Z")

</div>

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:

> <https://gist.github.com/CodingSpiderFox/eb22f5b7a8c4af89150e9d3043062c87>

Thank you in advance 🙂

---

<div class="post-metadata">

**Author:** ![davidcgu](https://avatars.discourse-cdn.com/v4/letter/d/e8c25b/32.png) [@davidcgu](https://discourse.nodered.org/u/davidcgu)\
**Post date:** [30 June 2018 21:05 UTC](https://discourse.nodered.org/t/node-red-making-http-requests-when-starting-up/1211/2 "2018-06-30T21:05:51Z")

</div>

Take a look on this:  
[https://flows.nodered.org/flow/a886a507c57ea83a3a5260338091505b](https://flows.nodered.org/flow/a886a507c57ea83a3a5260338091505b)

or

> **[node-red-contrib-persist](https://flows.nodered.org/node/node-red-contrib-persist)**
>
> A node-red node to persist data between Node Red deploys and restarts.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [30 June 2018 21:19 UTC](https://discourse.nodered.org/t/node-red-making-http-requests-when-starting-up/1211/3 "2018-06-30T21:19:40Z")

</div>

> [@cdfx](#):
>
> 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.

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

---

<div class="post-metadata">

**Author:** ![cdfx](https://avatars.discourse-cdn.com/v4/letter/c/e36b37/32.png) [@cdfx](https://discourse.nodered.org/u/cdfx)\
**Post date:** [30 June 2018 21:52 UTC](https://discourse.nodered.org/t/node-red-making-http-requests-when-starting-up/1211/4 "2018-06-30T21:52:51Z")

</div>

@Colin: You mean in the Sonoff configuration?

---

<div class="post-metadata">

**Author:** ![cdfx](https://avatars.discourse-cdn.com/v4/letter/c/e36b37/32.png) [@cdfx](https://discourse.nodered.org/u/cdfx)\
**Post date:** [30 June 2018 21:55 UTC](https://discourse.nodered.org/t/node-red-making-http-requests-when-starting-up/1211/5 "2018-06-30T21:55:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![davidcgu](https://avatars.discourse-cdn.com/v4/letter/d/e8c25b/32.png) [@davidcgu](https://discourse.nodered.org/u/davidcgu)\
**Post date:** [30 June 2018 21:59 UTC](https://discourse.nodered.org/t/node-red-making-http-requests-when-starting-up/1211/6 "2018-06-30T21:59:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [1 July 2018 07:13 UTC](https://discourse.nodered.org/t/node-red-making-http-requests-when-starting-up/1211/7 "2018-07-01T07:13:17Z")

</div>

> [@cdfx](#):
>
> 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

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

---

<div class="post-metadata">

**Author:** ![cdfx](https://avatars.discourse-cdn.com/v4/letter/c/e36b37/32.png) [@cdfx](https://discourse.nodered.org/u/cdfx)\
**Post date:** [1 July 2018 09:19 UTC](https://discourse.nodered.org/t/node-red-making-http-requests-when-starting-up/1211/8 "2018-07-01T09:19:52Z")

</div>

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”)

---

<div class="post-metadata">

**Author:** ![davidcgu](https://avatars.discourse-cdn.com/v4/letter/d/e8c25b/32.png) [@davidcgu](https://discourse.nodered.org/u/davidcgu)\
**Post date:** [1 July 2018 09:35 UTC](https://discourse.nodered.org/t/node-red-making-http-requests-when-starting-up/1211/9 "2018-07-01T09:35:31Z")

</div>

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

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/9010b6c42ecea900b19e71d52571241d047cc1a8.png)

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/09825c6bc46c4bc6a6742ab0d2235e6bd0fbbf46.png)

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

Regards

---

<div class="post-metadata">

**Author:** ![cdfx](https://avatars.discourse-cdn.com/v4/letter/c/e36b37/32.png) [@cdfx](https://discourse.nodered.org/u/cdfx)\
**Post date:** [1 July 2018 09:44 UTC](https://discourse.nodered.org/t/node-red-making-http-requests-when-starting-up/1211/10 "2018-07-01T09:44:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![davidcgu](https://avatars.discourse-cdn.com/v4/letter/d/e8c25b/32.png) [@davidcgu](https://discourse.nodered.org/u/davidcgu)\
**Post date:** [1 July 2018 09:50 UTC](https://discourse.nodered.org/t/node-red-making-http-requests-when-starting-up/1211/11 "2018-07-01T09:50:51Z")

</div>

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?

> **[arendst/Tasmota](https://github.com/arendst/Tasmota#mqtt-specific)**
>
> Alternative firmware for ESP8266 with easy configuration using webUI, OTA updates, automation using timers or rules, expandability and entirely local control over MQTT, HTTP, Serial or KNX. Full do...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/b98e2e400b66e6e8c737d92876651bf80ac167a0.png)

---

<div class="post-metadata">

**Author:** ![cdfx](https://avatars.discourse-cdn.com/v4/letter/c/e36b37/32.png) [@cdfx](https://discourse.nodered.org/u/cdfx)\
**Post date:** [2 July 2018 06:28 UTC](https://discourse.nodered.org/t/node-red-making-http-requests-when-starting-up/1211/12 "2018-07-02T06:28:57Z")

</div>

PowerRetain is set to off

---

<div class="post-metadata">

**Author:** ![cdfx](https://avatars.discourse-cdn.com/v4/letter/c/e36b37/32.png) [@cdfx](https://discourse.nodered.org/u/cdfx)\
**Post date:** [2 July 2018 06:52 UTC](https://discourse.nodered.org/t/node-red-making-http-requests-when-starting-up/1211/13 "2018-07-02T06:52:12Z")

</div>

Your solution with the global variable seems to work 🙂

Thanks a lot

---

<div class="post-metadata">

**Author:** ![cdfx](https://avatars.discourse-cdn.com/v4/letter/c/e36b37/32.png) [@cdfx](https://discourse.nodered.org/u/cdfx)\
**Post date:** [2 July 2018 07:14 UTC](https://discourse.nodered.org/t/node-red-making-http-requests-when-starting-up/1211/14 "2018-07-02T07:14:38Z")

</div>

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” ?

---

<div class="post-metadata">

**Author:** ![davidcgu](https://avatars.discourse-cdn.com/v4/letter/d/e8c25b/32.png) [@davidcgu](https://discourse.nodered.org/u/davidcgu)\
**Post date:** [2 July 2018 08:28 UTC](https://discourse.nodered.org/t/node-red-making-http-requests-when-starting-up/1211/15 "2018-07-02T08:28:43Z")

</div>

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:

```auto
[{"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"]]}]

```

---

<div class="post-metadata">

**Author:** ![cdfx](https://avatars.discourse-cdn.com/v4/letter/c/e36b37/32.png) [@cdfx](https://discourse.nodered.org/u/cdfx)\
**Post date:** [3 July 2018 19:53 UTC](https://discourse.nodered.org/t/node-red-making-http-requests-when-starting-up/1211/16 "2018-07-03T19:53:20Z")

</div>

> [@davidcgu](#):
>
> to

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.

---

<div class="post-metadata">

**Author:** ![davidcgu](https://avatars.discourse-cdn.com/v4/letter/d/e8c25b/32.png) [@davidcgu](https://discourse.nodered.org/u/davidcgu)\
**Post date:** [3 July 2018 20:07 UTC](https://discourse.nodered.org/t/node-red-making-http-requests-when-starting-up/1211/17 "2018-07-03T20:07:47Z")

</div>

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

> **[Installing MQTT Broker(Mosquitto) on Raspberry Pi](https://www.instructables.com/id/Installing-MQTT-BrokerMosquitto-on-Raspberry-Pi/)**
>
> Installing MQTT Broker(Mosquitto) on Raspberry Pi : In this instructables you will find how to install MQTT on Raspberry Pi. There are two methods to install mqtt, one is we directly connect keyboard mouse and monitor to a Pi , turn it on and go...

Regards

---

<div class="post-metadata">

**Author:** ![cdfx](https://avatars.discourse-cdn.com/v4/letter/c/e36b37/32.png) [@cdfx](https://discourse.nodered.org/u/cdfx)\
**Post date:** [4 July 2018 08:55 UTC](https://discourse.nodered.org/t/node-red-making-http-requests-when-starting-up/1211/18 "2018-07-04T08:55:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [4 July 2018 09:26 UTC](https://discourse.nodered.org/t/node-red-making-http-requests-when-starting-up/1211/19 "2018-07-04T09:26:12Z")

</div>

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.
