Opening and closing Garage door with Node-Red and MQTT

Hi all,
I am entirely new to Node-Red, JSON, Javascript or programming in general.
I'm hoping someone can help with what on the face of it seems a very straight forward thing to do, although for now, it's waaay beyond my knowledge and skills.

What I want to do is open and close the garage door using the following.

  1. MQTT and Node-RED runnning on a Pi (both currently working fine)
  2. A Shelly 1 relay - this works fine via the shelly app (I can open and close the door no problem and can also read the status of whether or not the door is closed or open by using a reed relay to detect with the Shelly1 SW input when the door is fully closed).
  3. Sonoff RF bridge flashed with Tasmota
  4. 433 Mhz sonoff switch (This is a momentary switch which generates a single data code when pressed)

Current status with using Node-RED:
I have the Sonoff switch talking to the RF bridge and detecting when the sonoff switch button is pressed
I can use an MQTT input to recognise when the sonoff switch button has been pressed.
I can use an MQTT input to determine whether or not the Garage door is open or closed
I can send a command to the Shelly via MQTT to open or close the garage door

What I need to be able to do is:

On the press of the sonoff switch

  1. Check the status of the garage door: Is it open or closed
    Door is closed: shellies/shelly1-Exxxxx/input/0=1
    Door is open: shellies/shelly1-Exxxxx/input/0=0

  2. Depending on the door status send an MQTT command to the Shelly to either close the door if its open or open the door if its closed. (shellies/shelly1-Exxxxx/relay/0/command=on)

I've searched the web and youtube for an example I can copy but not found anything I can use or make work.
Could somebody please either point me in the direction of a tutorial I can copy or give me some pointers to what nodes I should use?

Thanks in advance ...

How do you determine the status of the open/closed ? As mqtt only emits based on changes.

I would use a context variable to set the status of the door and read that to determine to send the open/close.


[{"id":"3ab09351.34ddf4","type":"inject","z":"de7fbcb7.677b2","name":"","topic":"","payload":"open","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":116,"y":132,"wires":[["757d758b.b6cbc4"]]},{"id":"49d153bc.15e7cc","type":"switch","z":"de7fbcb7.677b2","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"open","vt":"str"},{"t":"eq","v":"close","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":336,"y":352,"wires":[["a53daf4c.10d0c"],["5b752824.cedc48"]]},{"id":"757d758b.b6cbc4","type":"change","z":"de7fbcb7.677b2","name":"","rules":[{"t":"set","p":"garagedoorstatus","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":338,"y":154,"wires":[[]]},{"id":"fff52113.05b64","type":"inject","z":"de7fbcb7.677b2","name":"","topic":"","payload":"close","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":116,"y":176,"wires":[["757d758b.b6cbc4"]]},{"id":"1d8a638a.35021c","type":"inject","z":"de7fbcb7.677b2","name":"","topic":"","payload":"garagedoorstatus","payloadType":"flow","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":166,"y":352,"wires":[["49d153bc.15e7cc"]]},{"id":"450c7620.37028","type":"comment","z":"de7fbcb7.677b2","name":"set door status (from mqtt)","info":"","x":154,"y":88,"wires":[]},{"id":"5f15aafd.61e40c","type":"comment","z":"de7fbcb7.677b2","name":"flow triggered by sonoff switch input","info":"","x":184,"y":286,"wires":[]},{"id":"88a1311d.d191f8","type":"comment","z":"de7fbcb7.677b2","name":"to shelly switch (mqtt)","info":"","x":826,"y":286,"wires":[]},{"id":"a53daf4c.10d0c","type":"change","z":"de7fbcb7.677b2","name":"shellies/shelly1-Exxxxx/input/0=1","rules":[{"t":"set","p":"topic","pt":"msg","to":"shellies/shelly1-Exxxxx/input/0=1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":558,"y":330,"wires":[["5ff5d9a5.c56de8"]]},{"id":"5b752824.cedc48","type":"change","z":"de7fbcb7.677b2","name":"shellies/shelly1-Exxxxx/input/0=0","rules":[{"t":"set","p":"topic","pt":"msg","to":"shellies/shelly1-Exxxxx/input/0=0","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":558,"y":374,"wires":[["5ff5d9a5.c56de8"]]},{"id":"5ff5d9a5.c56de8","type":"debug","z":"de7fbcb7.677b2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":798,"y":352,"wires":[]}]

Hi bakman2,
Thanks for the feedback.

The Shelly1 publishes its status via MQTT every 30 seconds.
Hence, I can capture the status of the door by subscribing to: shellies/shelly1-Exxxxx/input/0
If its a 1 its closed, if its a 0 its open.

Thanks for the example, its given me a good idea on how to crack the problem.

Much appreciated.

How are you actually setting the variable? Do you have a contact switch or something of the sort wired in, or are you just flip-flopping the variable with every actuation? I caution against the latter, because it leaves you open to desync issues.

Hi Jay,
Thanks for the feedback and caution - appreciate you letting me know - I hadn't thought of that :slight_smile:
In this case its fine as I capture the state of the door (Open or Closed) via a hard wired reed relay. This is connected to 24v from the PSU for the Shelly1 and terminates on the Shelly1 SW input.
When the door is closed the reed relay makes and the SW input recognises 24V coming in and sets shellies/shelly1-Exxxxx/input/0=1

This then is published by the Shelly1 to MQTT every 30 seconds