-
Autor
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