creating a 3 way switch for my pool pump

  • Hi I am new to the shelly community.

    I need a bit of help with some shelly scripting.

    I have a shelly 3 Pro I am using in my pool shed to turn on and off a pool pump. Basic stuff really. I am using Home assistant as my automation server which runs all on and off times that need to be automated. and I was using it to control 2 manual states of the Pump: Manual On (ignores all automations and simply turns the pump on), Manual Off (ignores all automations and simply turns the pump off).

    Both of these states are crucial as I have a third party who cleans my pool and they need the ability to operate the pump independently of any computer controlled schedules.

    While Home assistant can cover the manual on and manual off scenarios, I have found two things which I am hoping scripting the same locally on the shelly device itself will resolve.

    1, there is a significant delay while the shelly communicates to Home Assistant and then Home assistant replies with the correct command. This delay causes confusion at the physical switch. and I have found the pool guy turning the switch on and off repeatedly scratching his head wondering why nothing is happening.

    2. if the shelly drops internet connection the manual switching conditions I have set in Home assistant stops working.

    The physical layout looks like this:

    Output zero of the shelly is the physical relay for the pump.

    Output 1 is connected to a green LED lamp which is displayed in the pump house

    Output 2 is connected to a Red LED lamp which is displayed in the pump house

    Input zero is not wired

    Input 1 is wired to one side of a physical 3 way switch (On-Off-On) connected to the shelly in the pump house.

    Input 2 is wired to the other switch of the same 3 way switch (On-Off-On) connected to the shelly in the pump house.

    In Home assistant code what happens when the physical 3 way switch is operated.

    If shelly Input 1 is on - turn on shelly Output zero

    If shelly Input 2 is on - turn Off shelly Output zero. Because this is a physical 3 way switch, when one position is chosen, the others are physically turned off.

    if the 3rd position of the switch is chosen. this sets a condition (Input 1 and 2 are both off) Home Assistant checks for, before it allows any automated timings to start the pump by turning on output zero, or stop the pump by turn off output zero.

    Shelly Script goal.

    My goal is to replace this same code from Home assistant as a script on the Shelly itself.

    here is where I have got with my shelly code so far.

    [script] [/script]

    [script]Shelly.addEventHandler(function (event) { [/script]

    [script]//check if the event source is an input [/script]

    [script]//and if the id of the input is 1 [/script]

    [script]if (event.name === "input" && event.id === 1 && event.info.state === true) { [/script]

    [script]//use event.info.state to determine the input state [/script]

    [script]//true - input on [/script]

    [script]//false - input off [/script]

    [script] Shelly.call("Switch.Set", "{ id: 0 " + Settings.Relay + ", on:" + JSON.stringify(newStatus) + "}", null, null); [/script]

    [script]} else if (event.name === "input" && event.id === 2 && event.info.state === true) { [/script]

    [script] Shelly.call("Switch.Set", "{ id: 0 " + Settings.Relay + ", on:" + JSON.stringify(newStatus) + "}", null, null); [/script]

    [script]} else{ [/script]

    [script] print("neither input is on, no change [/script]

    [script]} [/script]

    [script]}); [/script]

    [script][/script]

    My Web interface on my shelly keeps dropping out so I am having trouble getting results and or errors from my scripting returned. Can someone who knows take a look and establish if I am heading in the correct direction with this code?

    To pre-empt the question why am I using a Shelly 3 for this. that is because its the only shelly I could find which has the ability to drive the 3 switch conditions I need:

    Manual on

    Manual off

    Fully Automated.

    Kind regards

    Duncan

  • Hi All I believe I have answered my own question.

    The following code links the 3 shelly switches together and allows the manual states of a 3 way On-OFF-On switch to be emulated by the shelly 3 Pro in code. So far the result is loads faster than going via Home Assistant. There are some occasions the shelly presents a small time delay between manual switch operations, but it's far less noticeable than when HA was managing these same conditions.

    CODE I have working:

    [script][/script]

    [script]Shelly.addEventHandler(function (event) { [/script]

    [script] //check if the event source is an input [/script]

    [script] //and if the id of the input is 1 [/script]

    [script] if (event.name === "switch" && event.id === 1) { [/script]

    [script] //use event.info.state to determine the input state [/script]

    [script] //true - input on [/script]

    [script] //false - input off [/script]

    [script] //The print statements help determine where in the code the trigger lands [/script]

    [script] print("Switch ", event.id, " is ", event.info.state ? "on" : "off"); [/script]

    [script]
    [/script]

    [script] print (event.info.state); [/script]

    [script]
    [/script]

    [script] if (event.info.state === false){[/script]

    [script] print("sw1 event state is false"); [/script]

    [script] return ; [/script]

    [script] } else if (event.info.state === true){ [/script]

    [script] print("sw1 event state is true"); [/script]

    [script] Shelly.call("Switch.Set", {"id": 0, "on": true}); [/script]

    [script] } else { [/script]

    [script] print("hit sw1 lese statement"); [/script]

    [script] return ; [/script]

    [script] } [/script]

    [script] } else if (event.name === "switch" && event.id === 2) { [/script]

    [script] //use event.info.state to determine the input state [/script]

    [script] //true - input on [/script]

    [script] //false - input off [/script]

    [script] print("Switch ", event.id, " is ", event.info.state ? "on" : "off"); [/script]

    [script]
    [/script]

    [script] print (event.info.state); [/script]

    [script]
    [/script]

    [script] if (event.info.state === false){ [/script]

    [script] print("sw2 event state is false"); [/script]

    [script] return ; [/script]

    [script] } else if (event.info.state === true){ [/script]

    [script] print("sw2 event state is true"); [/script]

    [script] Shelly.call("Switch.Set", {"id": 0, "on": false}); [/script]

    [script] } else { [/script]

    [script] print("hit sw2 lese statement"); [/script]

    [script] return ; [/script]

    [script] } [/script]

    [script] } else{ [/script]

    [script] return ; [/script]

    [script] } [/script]

    [script]});[/script]

    [script]
    [/script]

    [script][/script]

    I hope this helps someone else out.

    Thanks Duncan