jneilliii / OctoPrint-TasmotaMQTT

18 stars 16 forks source link

Invert Relay for Normally Closed SONOFF Relay #17

Closed W4v3runn3r closed 2 years ago

W4v3runn3r commented 5 years ago

Awesome plug-in, great replacement for PSU Control when I moved from using a GPIO connected Relay on my PI to a SONOFF 4CH Pro.

Would it be possible to add an "Invert Relay" feature to your Relay Editor configuration settings? Basically ON would mean OFF and OFF would mean ON for that relay..

I used a "Normally Closed" relay setup on all my 3DPrinters. That way if the SONOFF were to lose power during a print, the mains source would continue to power the printer as the relay remains closed. I then Open or set to "ON" the relay via MQTT to turn OFF the printer.

I know everyone will tell me to set the SONOFF to "BASIC" and manually configure the Relay GPIO pin to invert for "Normally Closed" but then you lose all the great features/benefits of the SONOFF configuration.

I manually edited the case statement in your .js file and swapped the ON/OFF in the .css file. Now the plugin interprets ON/OFF swapped. Of course, this affects any relay configure in the plug-in versus any one specific relay. It would be great if this could be configure for a specific relay; that way you could have other relays in the plug-in that maintain their normal ON/OFF for a Normally Open relay setting. For example, if you had another relay that drove LED lights attached to your 3D Printer.

I would be very happy to test a branch if you have interested in pursuing the feature.

Keep up the great work.

jneilliii commented 5 years ago

This would require a significant amount of programming change to invert the logic based on a flag. However, for me to understand how you're using the sonoff, could you post a screenshot/diagrams of your sonoff configuration so I can emulate those settings? Are you using GPIO from the pi to the sonoff to emulate a button press? Sorry just confused how if your sonoff loses power your printer wouldn't also lose mains power.

W4v3runn3r commented 5 years ago

Away from my computer at the moment. I will post up a couple pictures of the Sonoff 4 channel pro wiring asap. This model has 4 relays; each relay can be wired as Normally Open or Normally Closed. If a Normally Open relay is de-energized the relay will open and the mains power will stop flowing. If a Normally Closed relay is de-energized the relay fails closed and continues to supply mains voltage. By using the relay in Normally Closed configuration I ensure the loss of power to the SONOFF controller will ensure the relay servicing my printer fails closed and continues to supply power to the printer. Saves me from failed print job in the case of someone power cycling the SONOFF controller. This mode is commonly referred to as an inverted relay. Another example would be a secure door lock. The normally closed relay would ensure the door remains locked if the door controller were to lose power.

To be clear, I am not using any GPIO pins with the SONOFF. Simply using MQTT to control the relays on the SONOFF. The sonoff is flashed with Tasmota.

Hope the extra detail helps. I will get those pics as soon as possible.

Not being a programmer, I kinda forget that something that seems simple on the surface can result in a lot of code to achieve. :-)

jneilliii commented 5 years ago

Thanks, that helps me understand better the need for this option. I didn't realize that the sonoff's had different wiring scenarios to the relays themselves, probably because I've only dealt with the single relay and plug devices. I assume that NC option is part of the 4-ch pro feature set. Next time I have time I'll look into incorporating this option to see how difficult it really would be.

W4v3runn3r commented 5 years ago

20181226_183656 20181226_183608 Here are a couple pictures of the SONOFF 4 Channel Relay. I have the first relay wired in Normally Closed (NC). When the relay is activated the LED is on, and the relay is open. Common wire goes in the middle post. The left post is Normally Open (NO), the right post is NC. The standard SONOFF switches don't have a NC option; they are always NO.

jneilliii commented 5 years ago

Makes total sense. Only question, is if you submit a status request to MQTT is it what I would expect where the topic %prefix%/%topic%/POWER1 returns a message of OFF when the printer is powered on because of the NC configuration and returns ON when the printer is powered off? Basically like you said, inverted from what you would expect.

I think it might be easy enough to change the color display by adding an inverted class to the icons, but that won't help with the actual toggling logic only put the icon in sync with the on/off state.

W4v3runn3r commented 5 years ago

You are correct. MQTT on Tasmota sends OFF when the printer is ON and ON when the printer is OFF. Unfortunately there is no OPTION setting in Tasmota that allows the sonoff configuration to be inverted for a relay. I would have to set the sonoff to 'generic' and do all the config manually. Defeats all the excellent features in Tasmota.

I handle this in Homeassistant by inverting the payload value of the MQTT state when it is received for that specific relay (POWER1). payload_on="OFF" payload_off="ON" Homeassistant creates a switch like a light and uses the payloads to display the current power state. Each relay is handled individually as they each have their own configuration profile.

Like I said, I was naive about your plugin. I thought there could be a case statement to check a relay's 'inverted' variable as on or off and then repeat your case statement but with ON and OFF switched where inverted variable is set to ON.

Not being a coder that may be kind of hacky.

jneilliii commented 3 years ago

I've added an inverted logic option in the above commit and will be available in version 0.3.9rc5 shortly. If you want to try it out you can change the plugin's release channel in OctoPrint's Software Update settings to Release Candidate and upgrade when prompted. Sorry it's taken so long to get this implemented.