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
-- 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
}
Attachments
Captura de pantalla 2023-03-18 120316.png (7.07 KiB) Viewed 1087 times
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
}
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
}
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
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
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.
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
}
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.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
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.