Failing to understand
Moderator: leecollings
-
- Posts: 29
- Joined: Monday 08 June 2020 6:41
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Failing to understand
Hello all
OK so dzVents has gotten the better of me. I've been attempting to write a script for weeks (7 weekends actually) and I'm just not getting it, so I'm giving up and posting here in the hope that someone will come to my rescue.
Here is what I am attempting to do:
If switch2 is on then do nothing (this is a failsafe switch and is normally off. If for any reason switch2 is turned on then switch1 should turn off and stay off.) I am monitoring switch2 and the system is sending me push notifications when it is turned on.
else
turn on switch1 every 4 hours
if switch3 or switch4 turn on then turn off switch1 (switch3 and switch4 are monitor switches and the action of switch1 should activate 1 or both of them)
write all actions to log so I can check that things are happening properly.
Simple huh. not so for me. I thought I would give programming a whirl. Now I know why I drive trucks instead.
If someone would be so kind as to post a solution to my problem I would be most grateful.
Regards
Fred
OK so dzVents has gotten the better of me. I've been attempting to write a script for weeks (7 weekends actually) and I'm just not getting it, so I'm giving up and posting here in the hope that someone will come to my rescue.
Here is what I am attempting to do:
If switch2 is on then do nothing (this is a failsafe switch and is normally off. If for any reason switch2 is turned on then switch1 should turn off and stay off.) I am monitoring switch2 and the system is sending me push notifications when it is turned on.
else
turn on switch1 every 4 hours
if switch3 or switch4 turn on then turn off switch1 (switch3 and switch4 are monitor switches and the action of switch1 should activate 1 or both of them)
write all actions to log so I can check that things are happening properly.
Simple huh. not so for me. I thought I would give programming a whirl. Now I know why I drive trucks instead.
If someone would be so kind as to post a solution to my problem I would be most grateful.
Regards
Fred
- habahabahaba
- Posts: 215
- Joined: Saturday 18 March 2023 14:44
- Target OS: Windows
- Domoticz version: 2024.4
- Contact:
Re: Failing to understand
I think you need 2 different scripts.
First will be triggered by devices (switch 1-4)
Second will be triggered by timer - every 4 hours to check status of switch1
First will be triggered by devices (switch 1-4)
Second will be triggered by timer - every 4 hours to check status of switch1
- user4563
- Posts: 108
- Joined: Tuesday 19 October 2021 17:24
- Target OS: Linux
- Domoticz version: 2024.7
- Contact:
Re: Failing to understand
Can you please paste your code - don't forget to format it as code so it appears correctly
ODroid H4+ / Ubuntu server
Aeotec Z-Stick Gen5+
ZWave-JS-UI
Domoticz for Android
Domoticz Scenes add-on in Kodi
Aeotec Z-Stick Gen5+
ZWave-JS-UI
Domoticz for Android
Domoticz Scenes add-on in Kodi
-
- Posts: 320
- Joined: Thursday 01 November 2018 19:47
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 2024.6
- Location: Portugal
- Contact:
Re: Failing to understand
Here's how I would start with this
Code: Select all
return {
on = {
devices = {
'Switch1',
'Switch2',
'Switch3',
'Switch4',
},
timer = {
'every 4 hours',
},
variables = {},
scenes = {},
groups = {},
security = {},
httpResponses = {},
shellCommandResponses = {},
customEvents = {},
system = {},
},
data = {},
logging = {},
execute = function(domoticz, item)
local dz = domoticz
local dzd = dz.devices
local switch1 = dzd('Switch1')
local switch2 = dzd('Switch2')
local switch3 = dzd('Switch3')
local switch4 = dzd('Switch4')
local chatter = item.isDevice and item.name or item.trigger
if chatter == 'Every 4 hours'
then
switch1.switchOn().checkFirst()
elseif (chatter == switch3 or chatter == switch4)
then
switch1.switchOff().checkFirst()
end
if switch2.state == 'Off'
then switch1.switchOff().checkFirst()
end
end
}
Intel NUC with Ubuntu Server VM (Proxmox),mosquitto(docker),RFXtrx433E,zwavejsUI (docker),Zigbee2mqtt(docker),SMA Hub (docker),Harmony Hub plugin, Kodi plugin,Homebridge(docker)+Google Home,APC UPS,SMA Modbus,Mitsubishi MQTT, Broadlink,Dombus
-
- Posts: 564
- Joined: Monday 20 March 2023 14:41
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Location: Somewhere in NL
- Contact:
Re: Failing to understand
Is this from ChatGPT, because it is not programming.
1. Why 4 switches?
2 this part is redundant.
3. This part
Could be changed to
The rest I did not test, but it seems to me that the test with the capital 'E' in
will never met the condition.
1. Why 4 switches?
2 this part is redundant.
Code: Select all
variables = {},
scenes = {},
groups = {},
security = {},
httpResponses = {},
shellCommandResponses = {},
customEvents = {},
system = {},
},
data = {},
logging = {},
Code: Select all
execute = function(domoticz, item)
local dz = domoticz
Code: Select all
execute = function(dz, item)
Code: Select all
if chatter == 'Every 4 hours'
Logic will get you from A to B. Imagination will take you everywhere.
-
- Posts: 320
- Joined: Thursday 01 November 2018 19:47
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 2024.6
- Location: Portugal
- Contact:
Re: Failing to understand
No it's not from chatgpt. Just my basic starting point. What made you think it was from chatgpt ?
Intel NUC with Ubuntu Server VM (Proxmox),mosquitto(docker),RFXtrx433E,zwavejsUI (docker),Zigbee2mqtt(docker),SMA Hub (docker),Harmony Hub plugin, Kodi plugin,Homebridge(docker)+Google Home,APC UPS,SMA Modbus,Mitsubishi MQTT, Broadlink,Dombus
-
- Posts: 320
- Joined: Thursday 01 November 2018 19:47
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 2024.6
- Location: Portugal
- Contact:
Re: Failing to understand
Well spotted. Anything more to add to help the OP ?Kedi wrote: ↑Monday 20 January 2025 15:46
The rest I did not test, but it seems to me that the test with the capital 'E' inwill never met the condition.Code: Select all
if chatter == 'Every 4 hours'
Intel NUC with Ubuntu Server VM (Proxmox),mosquitto(docker),RFXtrx433E,zwavejsUI (docker),Zigbee2mqtt(docker),SMA Hub (docker),Harmony Hub plugin, Kodi plugin,Homebridge(docker)+Google Home,APC UPS,SMA Modbus,Mitsubishi MQTT, Broadlink,Dombus
-
- Posts: 320
- Joined: Thursday 01 November 2018 19:47
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 2024.6
- Location: Portugal
- Contact:
Re: Failing to understand
It might not be good cleaned up code but a starting point for someone asking for help. Bar the "e" error it would most likely function.
Sometimes it is easy to see why people don't ask for assistance here. Perhaps some constructive criticism would might even make you feel better.
Intel NUC with Ubuntu Server VM (Proxmox),mosquitto(docker),RFXtrx433E,zwavejsUI (docker),Zigbee2mqtt(docker),SMA Hub (docker),Harmony Hub plugin, Kodi plugin,Homebridge(docker)+Google Home,APC UPS,SMA Modbus,Mitsubishi MQTT, Broadlink,Dombus
-
- Posts: 564
- Joined: Monday 20 March 2023 14:41
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Location: Somewhere in NL
- Contact:
Re: Failing to understand
The redundant stuff.
Like I wrote, the I did not test the logic, but it seems ok. The 'e' error might be considered constructive criticism

The OP should test it.
Logic will get you from A to B. Imagination will take you everywhere.
-
- Posts: 29
- Joined: Monday 08 June 2020 6:41
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: Failing to understand
Firstly, thanks heaps for the code as a starting point. I've just got home after a 14 hour day, so a little jaded, but had to give it a shot before a quick meal and bed.
Firstly there doesn't seem to be any logging so I can't debug it.
When I run the code by turning on switch1 it turns off immediately, and as I can't see anything in the log I don't know why it is turning off.
If I am testing it wrong by doing it that way instead of waiting for the timer then I can simply adjust the timer, but that has to be a tomorrow job.
Regards
Fred
-
- Posts: 320
- Joined: Thursday 01 November 2018 19:47
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 2024.6
- Location: Portugal
- Contact:
Re: Failing to understand
Could you clarify this, it seems do be 2 outcomes for 'On' but I'm not sure what conditions are required. Anyway, you could give this a try.
I've added logging, perhaps too much
Code: Select all
return {
on = {
devices = {
'Switch1',
'Switch2',
'Switch3',
'Switch4',
},
timer = {
'every 4 hours',
},
},
execute = function(domoticz, item)
local dz = domoticz
local dzd = dz.devices
local switch1 = dzd('Switch1')
local switch2 = dzd('Switch2')
local chatter = item.isDevice and item.name or item.trigger
dz.log('Script activated by '..chatter, dz.LOG_INFO)
if switch2.state == 'Off'
then
if chatter == 'switch2' --if switch2 activated the script
then
switch1.switchOff().silent() --stops the script triggering itself but you might need to alter this is switch1 is used to trigger another script
dz.log(chatter..' activated , switch 1 turning off if not already off', dz.LOG_INFO)
else dz.log('switch2 still On , do nothing', dz.LOG_INFO)
return --exits the script (do nothing) if switch2 is still on but wasn't just switched on
end
end
if chatter == 'every 4 hours'
then
dz.log('4 hour timer activated , switch 1 turning on if not already on', dz.LOG_INFO)
switch1.switchOn().checkFirst()
end
local switch3 = dzd('Switch3')
local switch4 = dzd('Switch4')
if chatter == 'every 4 hours'
then
if switch3.lastUpdate.secondsAgo < 1 --if turnng on switch1 caused switch3 or switch4 to turn on
then
dz.log('switch3 activated , turning off switch1', dz.LOG_INFO)
switch1.switchOff().silent()
elseif switch4.lastUpdate.secondsAgo < 1
then
dz.log('switch4 activated , turning off switch1', dz.LOG_INFO)
switch1.switchOff().silent()
end
end
end
}
I do understand your frustration, I work in construction myself.
Intel NUC with Ubuntu Server VM (Proxmox),mosquitto(docker),RFXtrx433E,zwavejsUI (docker),Zigbee2mqtt(docker),SMA Hub (docker),Harmony Hub plugin, Kodi plugin,Homebridge(docker)+Google Home,APC UPS,SMA Modbus,Mitsubishi MQTT, Broadlink,Dombus
-
- Posts: 630
- Joined: Saturday 21 September 2019 17:55
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 2024.1
- Location: The Netherlands
- Contact:
Re: Failing to understand
Before you dive into coding I think the above logic needs some further clarification:FredZ wrote: ↑Monday 20 January 2025 3:29 Hello all
OK so dzVents has gotten the better of me. I've been attempting to write a script for weeks (7 weekends actually) and I'm just not getting it, so I'm giving up and posting here in the hope that someone will come to my rescue.
Here is what I am attempting to do:
If switch2 is on then do nothing (this is a failsafe switch and is normally off. If for any reason switch2 is turned on then switch1 should turn off and stay off.) I am monitoring switch2 and the system is sending me push notifications when it is turned on.
else
turn on switch1 every 4 hours
if switch3 or switch4 turn on then turn off switch1 (switch3 and switch4 are monitor switches and the action of switch1 should activate 1 or both of them)
write all actions to log so I can check that things are happening properly.
Simple huh. not so for me. I thought I would give programming a whirl. Now I know why I drive trucks instead.
If someone would be so kind as to post a solution to my problem I would be most grateful.
Regards
Fred
1) You say "if switch2 is on then do nothing" but you also say "if switch2 is turned on then switch1 should turn off". This seems inconsistent: turning switch 1 off is not the same as "do nothing".
2) You say: if switch3 or switch 4 turn on then turn off switch 1. This part is not within the "else" section so you run the risk that switch1 is turned on by the 4hr timer and then immediately switched off because either sw3 or sw4 are on. Is that o.k.?
3) Between brackets you write that an action of switch1 should activate sw3,sw4 or both of them. But before the brackets you write the reverse, i.e. sw3 and sw4 set sw1, not the other way around. Please clarify.
4) is there anything that can set sw2 back to off? Probably outside the script? If not, then sw1 will always stay off.
5) Also sw3 and sw4 are trigger outside the script? If sw2,sw3 and sw4 are all off then sw1 is switched on and remains on until sw2 or sw3 or sw4 are switched on. Is that what you want?
-
- Posts: 564
- Joined: Monday 20 March 2023 14:41
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Location: Somewhere in NL
- Contact:
Re: Failing to understand
"if switch2 is on" is static --> so do nothing is valid
"if switch2 is turned on" is an action/trigger --> so "switch1 should turn off" is valid
Making a flowchart could help.
"if switch2 is turned on" is an action/trigger --> so "switch1 should turn off" is valid
Making a flowchart could help.
Logic will get you from A to B. Imagination will take you everywhere.
-
- Posts: 29
- Joined: Monday 08 June 2020 6:41
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: Failing to understand
Hopefully this will clarify what I am attempting to do.
The system is an automated header tank refiling system.
The pump (switch1) pumps water from a 35K litre tank up to a header tank several hundred meters up the hill.
The header tank sits in a trough to catch water that overflows from the header tank for any reason.
In the trough is a sensor (switch2) that detects if water exists in the trough. If water exists then don't turn the pump on.
On the header tank a sensor (switch3) detects when the water has reached the top of the tank. When the tank is full turn the pump off.
On the 35K tank there is a sensor (switch4) that detects when the water level is low. If the level is low turn the pump off.
switch2 sends a push notification to alert me that there is water in the trough.
switch4 sends me a push notification so that I can order a tanker if required.
I elected to regularly top up the header tank as it serves the house and all troughs for the animals as well the irrigation for our garden.
We also have a wastewater treatment plant to recover as much household water as possible rather than sending it to a septic tank and recover nothing. This system is 100% stand alone and will not be integrated into Dom.
Regards
Fred
The system is an automated header tank refiling system.
The pump (switch1) pumps water from a 35K litre tank up to a header tank several hundred meters up the hill.
The header tank sits in a trough to catch water that overflows from the header tank for any reason.
In the trough is a sensor (switch2) that detects if water exists in the trough. If water exists then don't turn the pump on.
On the header tank a sensor (switch3) detects when the water has reached the top of the tank. When the tank is full turn the pump off.
On the 35K tank there is a sensor (switch4) that detects when the water level is low. If the level is low turn the pump off.
switch2 sends a push notification to alert me that there is water in the trough.
switch4 sends me a push notification so that I can order a tanker if required.
I elected to regularly top up the header tank as it serves the house and all troughs for the animals as well the irrigation for our garden.
We also have a wastewater treatment plant to recover as much household water as possible rather than sending it to a septic tank and recover nothing. This system is 100% stand alone and will not be integrated into Dom.
Regards
Fred
-
- Posts: 320
- Joined: Thursday 01 November 2018 19:47
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 2024.6
- Location: Portugal
- Contact:
Re: Failing to understand
Much much easier now I know the application. I also run 1 borehole pump, 1 well pump and 1 pool pump all integrated into Domoticz.
This could work
Good luck !
This could work
Code: Select all
return {
on = {
devices = {
'Switch1',
'Switch2',
'Switch3',
},
timer = {
'every 4 hours',
},
},
execute = function(domoticz, item)
local dz = domoticz
local dzd = dz.devices
local pump = dzd('Switch1')
local overflow = dzd('Switch2')
local headerFull = dzd('Switch3')
local tankLow = dzd('Switch4')
local chatter = item.isDevice and item.name or item.trigger
dz.log('Header Tank Script triggered by '..chatter, dz.LOG_INFO)
if overflow.state == 'Off' and headerFull.state == 'Off' and tankLow.state == 'Off'
then
pump.switchOn()
domoticz.log('Pump turning On triggered by '..chatter,dz.LOG_FORCE)
else
pump.switchOff()
domoticz.log('Pump turning Off triggered by '..chatter,dz.LOG_FORCE)
end
if tankLow.state == 'On'
then
--put your notification method here
domoticz.log('Tank Low',dz.LOG_FORCE)
end
end
}
Intel NUC with Ubuntu Server VM (Proxmox),mosquitto(docker),RFXtrx433E,zwavejsUI (docker),Zigbee2mqtt(docker),SMA Hub (docker),Harmony Hub plugin, Kodi plugin,Homebridge(docker)+Google Home,APC UPS,SMA Modbus,Mitsubishi MQTT, Broadlink,Dombus
-
- Posts: 320
- Joined: Thursday 01 November 2018 19:47
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 2024.6
- Location: Portugal
- Contact:
Re: Failing to understand
You mentioned a 4 hour timer in a previous post, is that not needed ? I left a 4 hour trigger in that script anyway but you can delete it if you don't need it.
Intel NUC with Ubuntu Server VM (Proxmox),mosquitto(docker),RFXtrx433E,zwavejsUI (docker),Zigbee2mqtt(docker),SMA Hub (docker),Harmony Hub plugin, Kodi plugin,Homebridge(docker)+Google Home,APC UPS,SMA Modbus,Mitsubishi MQTT, Broadlink,Dombus
- habahabahaba
- Posts: 215
- Joined: Saturday 18 March 2023 14:44
- Target OS: Windows
- Domoticz version: 2024.4
- Contact:
Re: Failing to understand
Why Switch4 not trigger the script?
What if:
1. script was triggered by sw2-sw3 (status On) at 03:58
2. next script running is by timer at 04:00 - it will turn pump off because of state sw2-sw3 (see the point 1)
So the next cycles pump will be turned off everytime.
- waltervl
- Posts: 5389
- Joined: Monday 28 January 2019 18:48
- Target OS: Linux
- Domoticz version: 2024.7
- Location: NL
- Contact:
Re: Failing to understand
Switch1 (the pump) should not trigger anything, only a change in sensor state (switch 2,3,4) should trigger an action.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
-
- Posts: 320
- Joined: Thursday 01 November 2018 19:47
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 2024.6
- Location: Portugal
- Contact:
Re: Failing to understand
Ah yes, both true although I don't understand
The pump should not trigger the script but the tank low sensor should.What if:
1. script was triggered by sw2-sw3 (status On) at 03:58
2. next script running is by timer at 04:00 - it will turn pump off because of state sw2-sw3 (see the point 1)
So the next cycles pump will be turned off everytime.
Intel NUC with Ubuntu Server VM (Proxmox),mosquitto(docker),RFXtrx433E,zwavejsUI (docker),Zigbee2mqtt(docker),SMA Hub (docker),Harmony Hub plugin, Kodi plugin,Homebridge(docker)+Google Home,APC UPS,SMA Modbus,Mitsubishi MQTT, Broadlink,Dombus
-
- Posts: 29
- Joined: Monday 08 June 2020 6:41
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: Failing to understand
I've tested this script.
For testing purpose I set the timer to 10 minutes.
The pump turns on, the header tank fills. headerFull = dzd('Switch3') becomes active and the pump turns off. However as soon as the water level is low enough for the sensor to become inactive the pump turns back on.
Same applies to overflow = dzd('Switch2').
I have not tested tankLow = dzd('Switch4')
Yes these actions all happen inside the 10 minutes for the next timer.
It seems when the pump is turned on it stays on, the only thing that turns it off is the active state of the sensors.
Regards
Fred
For testing purpose I set the timer to 10 minutes.
The pump turns on, the header tank fills. headerFull = dzd('Switch3') becomes active and the pump turns off. However as soon as the water level is low enough for the sensor to become inactive the pump turns back on.
Same applies to overflow = dzd('Switch2').
I have not tested tankLow = dzd('Switch4')
Yes these actions all happen inside the 10 minutes for the next timer.
It seems when the pump is turned on it stays on, the only thing that turns it off is the active state of the sensors.
Regards
Fred
solarboy wrote: ↑Wednesday 22 January 2025 16:09 Much much easier now I know the application. I also run 1 borehole pump, 1 well pump and 1 pool pump all integrated into Domoticz.
This could workGood luck !Code: Select all
return { on = { devices = { 'Switch1', 'Switch2', 'Switch3', }, timer = { 'every 4 hours', }, }, execute = function(domoticz, item) local dz = domoticz local dzd = dz.devices local pump = dzd('Switch1') local overflow = dzd('Switch2') local headerFull = dzd('Switch3') local tankLow = dzd('Switch4') local chatter = item.isDevice and item.name or item.trigger dz.log('Header Tank Script triggered by '..chatter, dz.LOG_INFO) if overflow.state == 'Off' and headerFull.state == 'Off' and tankLow.state == 'Off' then pump.switchOn() domoticz.log('Pump turning On triggered by '..chatter,dz.LOG_FORCE) else pump.switchOff() domoticz.log('Pump turning Off triggered by '..chatter,dz.LOG_FORCE) end if tankLow.state == 'On' then --put your notification method here domoticz.log('Tank Low',dz.LOG_FORCE) end end }
Who is online
Users browsing this forum: No registered users and 1 guest