Page 1 of 1
turns on a switch every time the air sensor value is 12
Posted: Saturday 18 March 2023 19:05
by betogar
I want to make a script that turns on a switch every time the air sensor value is 12 or higher and turns off when it is 11 or less
I am using a custom sensor for air quality
try this but it gives error
Code: Select all
-- Define the name of the custom sensor and the name of the switch
local AQI_sensor_name = "AQI"
local power_switch_name = "power"
-- Function to turn on the switch
function turn_on()
commandArray[power_switch_name] = 'On'
print("Turning on " .. power_switch_name)
end
-- Function to turn off the switch
function turn_off()
commandArray[power_switch_name] = 'Off'
print("Turning off " .. power_switch_name)
end
-- Function to check if the switch is already on or off
function checkFirst()
local AQI_value = tonumber(otherdevices[AQI_sensor_name])
if AQI_value >= 12 and otherdevices[power_switch_name] == 'Off' then
turn_on()
elseif AQI_value <= 11 and otherdevices[power_switch_name] == 'On' then
turn_off()
end
end
-- Execute checkFirst on every update of the custom sensor
return {
on = {
devices = {
AQI_sensor_name
}
},
execute = function(dz, trigger)
checkFirst()
end
}
Re: turns on a switch every time the air sensor value is 12
Posted: Saturday 18 March 2023 21:06
by habahabahaba
try this (remove spaces. sorry, i don't how to insert normal code - it gives me mistakes):
Code: Select all
return {
on = {
devices = {
{'your_sensor_name'},
}
},
execute = function(dz)
local AQI = dz . devices(IDx) -- Idx - id of your AQI sensor
local powerSwitch = dz . devices(IDx) -- Idx id of your power switch
if AQI . value >=12 then
powerSwitch . switchOn() . checkFirst()
else
powerSwitch . switchOff() . checkFirst()
end
end
}
Re: turns on a switch every time the air sensor value is 12
Posted: Sunday 19 March 2023 3:03
by betogar
try with value 1, it doesn't work
nothing in log
Code: Select all
return {
on = {
devices = {
{'AQI'},
}
},
execute = function(dz)
local AQI = dz.devices(126) -- Idx - id of your AQI sensor
local powerSwitch = dz.devices(139) -- Idx id of your power switch
if AQI.value >=1 then
powerSwitch.switchOn().checkFirst()
else
powerSwitch.switchOff().checkFirst()
end
end
}
Re: turns on a switch every time the air sensor value is 12
Posted: Sunday 19 March 2023 6:19
by habahabahaba
Log?
Re: turns on a switch every time the air sensor value is 12
Posted: Sunday 19 March 2023 6:45
by habahabahaba
OK. try this one. Working code - just tested.
And
here is FAQ of DzVents.
Code: Select all
return {
on = {
timer = { 'every minute',
}
},
execute = function(domoticz , timer)
local AQI = domoticz .devices(40)
local switchBackyard = domoticz . devices(1)
AQI . updateCustomSensor(11) --just an example to set the value
if (AQI . sensorValue >= 12 ) then
switchBackyard . switchOn() . checkFirst()
else
switchBackyard . switchOff() . checkFirst()
end
domoticz . log(tostring(AQI . sensorValue)..' - AQI value', domoticz . LOG_ERROR)
end
}
Re: turns on a switch every time the air sensor value is 12
Posted: Sunday 19 March 2023 23:24
by betogar
It works but I see an error in the log, any ideas?
Code: Select all
return {
on = {
timer = { 'every minute',
}
},
execute = function(domoticz , timer)
local AQI = domoticz.devices(126)
local Power = domoticz.devices(139)
if (AQI.sensorValue >= 1 ) then
Power.switchOn().checkFirst()
else
Power.switchOff().checkFirst()
end
domoticz.log(tostring(AQI.sensorValue)..' - AQI value', domoticz.LOG_ERROR)
end
}
Re: turns on a switch every time the air sensor value is 12
Posted: Monday 20 March 2023 9:35
by waltervl
That is because you log it as an Error:
Code: Select all
domoticz.log(tostring(AQI.sensorValue)..' - AQI value', domoticz.LOG_ERROR)
should be
Code: Select all
domoticz.log(tostring(AQI.sensorValue)..' - AQI value', domoticz.LOG_INFO)
Re: turns on a switch every time the air sensor value is 12
Posted: Monday 20 March 2023 16:00
by betogar
it works! thanks guys!
Re: turns on a switch every time the air sensor value is 12
Posted: Thursday 01 June 2023 8:50
by BerryB
I tried the script but it gives an error: attempt to compare number with nil
What am I doing wrong here? Thanks in advance.
Ps. trying to figure out how to paste my code beneath.
Re: turns on a switch every time the air sensor value is 12
Posted: Thursday 01 June 2023 10:00
by waltervl
BerryB wrote: ↑Thursday 01 June 2023 8:50
I tried the script but it gives an error: attempt to compare number with nil
What am I doing wrong here? Thanks in advance.
Ps. trying to figure out how to paste my code beneath.
Some device value probably wrong in your script. But it is important to know in what line of your script the error occurs.
To post code, just copy paste and put it between code tags.
app.php/help/bbcode
Re: turns on a switch every time the air sensor value is 12
Posted: Thursday 01 June 2023 11:55
by BerryB
waltervl wrote: ↑Thursday 01 June 2023 10:00
BerryB wrote: ↑Thursday 01 June 2023 8:50
I tried the script but it gives an error: attempt to compare number with nil
What am I doing wrong here? Thanks in advance.
Ps. trying to figure out how to paste my code beneath.
Some device value probably wrong in your script. But it is important to know in what line of your script the error occurs.
To post code, just copy paste and put it between code tags.
.php/help/bbcode
When I try to copy & paste the code between code tags I get this message:
You can’t post image, email or url links that are external to this domain. Please remove domoticz., domoticz., Tempsensor., Power., Power., domoticz., Tempsensor. and domoticz.
Thanks for the help
Re: turns on a switch every time the air sensor value is 12
Posted: Thursday 01 June 2023 23:58
by waltervl
Probably because you are a new user. Are there urls in the code?
Re: turns on a switch every time the air sensor value is 12
Posted: Friday 02 June 2023 9:45
by BerryB
waltervl wrote: ↑Thursday 01 June 2023 23:58
Probably because you are a new user. Are there urls in the code?
No there are no URL's.
I think I am this close solving the problem.
Maybe I'm not giving the temp sensor a "start temp"?
####.updateTemperature(11)
error is : attempt to compare nil with number
The ### is in my script the name of the device defined in local. But cant post because "they are external"
Re: turns on a switch every time the air sensor value is 12
Posted: Friday 02 June 2023 11:19
by BerryB
I think I solved it. Don't know exactly if I did the repeat every minute ok?
Code: Select all
local switchDeviceID = 135 -- Vervang 123 door het werkelijke apparaat-ID van de schakelaar
return {
on = {
timer = {
'every minute'
}
},
on = {
devices = {
'SonofGTemperatuur' -- Vervang 'temperatuursensor' door het werkelijke apparaat-ID van de temperatuursensor
}
},
execute = function(domoticz,device)
local temperatureThreshold = 16
if device.temperature <= temperatureThreshold then
domoticz.devices(switchDeviceID).switchOn()
domoticz.log("Temperatuur boven de " .. temperatureThreshold .. " graden. Schakelaar is geactiveerd.")
else
domoticz.devices(switchDeviceID).switchOff()
domoticz.log("Temperatuur onder de " .. temperatureThreshold .. " graden. Schakelaar is gedeactiveerd.")
end
end
}
Re: turns on a switch every time the air sensor value is 12
Posted: Friday 02 June 2023 11:34
by waltervl
The 'every minute' part should be removed as it will mix up the device setup:
in 'execute = function(domoticz,device)' the device parameter is filled with the triggered device. When execute is triggered by the timer, device is a timer object and the declaration of device.temperature will fail as a timer has no temperature and giving nil.
Re: turns on a switch every time the air sensor value is 12
Posted: Friday 02 June 2023 13:44
by BerryB
waltervl wrote: ↑Friday 02 June 2023 11:34
The 'every minute' part should be removed as it will mix up the device setup:
in 'execute = function(domoticz,device)' the device parameter is filled with the triggered device. When execute is triggered by the timer, device is a timer object and the declaration of device.temperature will fail as a timer has no temperature and giving nil.
Thanks 4 the help.