turns on a switch every time the air sensor value is 12

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
betogar
Posts: 6
Joined: Saturday 10 December 2022 23:49
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

turns on a switch every time the air sensor value is 12

Post 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
}
Attachments
Captura de pantalla 2023-03-18 120316.png
Captura de pantalla 2023-03-18 120316.png (7.07 KiB) Viewed 1084 times
User avatar
habahabahaba
Posts: 232
Joined: Saturday 18 March 2023 14:44
Target OS: Windows
Domoticz version: 2024.4
Contact:

Re: turns on a switch every time the air sensor value is 12

Post 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
} 
betogar
Posts: 6
Joined: Saturday 10 December 2022 23:49
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: turns on a switch every time the air sensor value is 12

Post 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
} 

Attachments
Capdtura.PNG
Capdtura.PNG (11.67 KiB) Viewed 1064 times
User avatar
habahabahaba
Posts: 232
Joined: Saturday 18 March 2023 14:44
Target OS: Windows
Domoticz version: 2024.4
Contact:

Re: turns on a switch every time the air sensor value is 12

Post by habahabahaba »

Log?
User avatar
habahabahaba
Posts: 232
Joined: Saturday 18 March 2023 14:44
Target OS: Windows
Domoticz version: 2024.4
Contact:

Re: turns on a switch every time the air sensor value is 12

Post 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
}
betogar
Posts: 6
Joined: Saturday 10 December 2022 23:49
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: turns on a switch every time the air sensor value is 12

Post 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
}


Attachments
err.png
err.png (8.21 KiB) Viewed 1022 times
User avatar
waltervl
Posts: 5777
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: turns on a switch every time the air sensor value is 12

Post 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)
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
betogar
Posts: 6
Joined: Saturday 10 December 2022 23:49
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: turns on a switch every time the air sensor value is 12

Post by betogar »

it works! thanks guys!
BerryB
Posts: 5
Joined: Wednesday 31 May 2023 17:12
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: turns on a switch every time the air sensor value is 12

Post 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.
User avatar
waltervl
Posts: 5777
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: turns on a switch every time the air sensor value is 12

Post 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
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
BerryB
Posts: 5
Joined: Wednesday 31 May 2023 17:12
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: turns on a switch every time the air sensor value is 12

Post 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
User avatar
waltervl
Posts: 5777
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: turns on a switch every time the air sensor value is 12

Post by waltervl »

Probably because you are a new user. Are there urls in the code?
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
BerryB
Posts: 5
Joined: Wednesday 31 May 2023 17:12
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: turns on a switch every time the air sensor value is 12

Post 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"
BerryB
Posts: 5
Joined: Wednesday 31 May 2023 17:12
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: turns on a switch every time the air sensor value is 12

Post 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
}
User avatar
waltervl
Posts: 5777
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: turns on a switch every time the air sensor value is 12

Post 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.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
BerryB
Posts: 5
Joined: Wednesday 31 May 2023 17:12
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: turns on a switch every time the air sensor value is 12

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest