Page 1 of 1

Start script when UV changes

Posted: Saturday 25 July 2020 14:27
by wouterlet
I made a script for the shutters in our bedrooms. They have 3 states 'open' 'close', 'stop'. Stop is a state with small holes so the wind can get trough.
My problem at the moment is I would like the script to start when the UV from Accuweather changes. The value of UV.uv works, but this local is made in the script so I can't use it to start the script. Is there a way to make this happen?
Thanks in advance!



Code: Select all

return {
	on = {
		timer = {'at 9:00', 'at 12:00', 'at 19:01', 'at 21:00'},
		devices = {'UV Accuweather', 'Raam Ons Links', 'Raam Ons Rechts', 'Raam Ivo', 'Raam Pim'}
		},
    execute = function(dz, timer)
    local UV = dz.devices('UV Accuweather')
    local VarPimIvo = dz.variables('Var Luik Pim Ivo')
    local VarOns = dz.variables('Var Luik Ons')
	if  dz.time.matchesRule('at 09:00') or 
	    (UV.uv  < 2 and 
	    dz.time.matchesRule('at 09:00-19:00') ) then
	    dz.devices('Gordijn Ivo').switchOff()
	    dz.devices('Gordijn Pim').switchOff()

    elseif  dz.time.matchesRule('at 09:00') and
        dz.time.matchesRule('on mon, tue, wed, thu, fri') then
	    dz.devices('Gordijn Ons').switchOff()

elseif  dz.time.matchesRule('at 12:00-18:00') and
        UV.uv < 2 then
	    dz.devices('Gordijn Ons').switchOff()

    elseif  dz.time.matchesRule('at 12:00-18:00') and  
        UV.uv > 1 and (
	    dz.devices('Raam Ivo').state == 'Open' or	    
	    dz.devices('Raam Pim').state == 'Open' ) then 
        dz.devices('Gordijn Ivo').stop()
	    dz.devices('Gordijn Pim').stop()

    elseif  dz.time.matchesRule('at 12:00-19:00') and  
        UV.uv > 1 and (
	    dz.devices('Raam Ons Rechts').state == 'Open' or	    
	    dz.devices('Raam Ons Links').state == 'Open' ) then 
        dz.devices('Gordijn Ons').stop()

    elseif  dz.time.matchesRule('at 12:00-18:00') and  
        UV.uv > 1 and 
        dz.devices('Raam Ivo').state == 'Closed' and 
        dz.devices('Raam Pim').state == 'Closed' then 
        dz.devices('Gordijn Ivo').switchOn()
	    dz.devices('Gordijn Pim').switchOn()

    elseif  dz.time.matchesRule('at 12:00-19:00') and  
        UV.uv > 1 and
	    dz.devices('Raam Ons Rechts').state == 'Closed' and	    
	    dz.devices('Raam Ons Links').state == 'Closed' then 
        dz.devices('Gordijn Ons').switchOn()

	elseif  dz.time.matchesRule('at 19:01') then
	    dz.devices('Gordijn Ivo').switchOn()
	    dz.devices('Gordijn Pim').switchOn()

	elseif  dz.time.matchesRule('at 21:00') then
	    dz.devices('Gordijn Ons').switchOn()
		end		
	end
}

Re: Start script when UV changes

Posted: Saturday 25 July 2020 14:50
by waaren
wouterlet wrote: Saturday 25 July 2020 14:27 My problem at the moment is I would like the script to start when the UV from Accuweather changes.
The script will be triggered at the given times and on every update of the given devices. Regardless if the values changed or not.
You need to store the previous value of the UV sensor somewhere to prevent the rest of the script from executing when the UV value did not change. You can use dzVents persistent data for that. It then could look like below.

Code: Select all

return 
{
	on = 
	{
		timer = 
		{	'at 9:00', 
			'at 12:00', 
			'at 19:01', 
			'at 21:00',
		},
		
		devices = 
		{
			'UV Accuweather', 
			'Raam Ons Links', 
			'Raam Ons Rechts', 
			'Raam Ivo', 
			'Raam Pim',
		}
	},
	
	logging = 
	{
		level = domoticz.LOG_DEBUG,
		marker = 'Bedroom shutters', 
	},
	
	data =
	{
		UV = 
		{ 
			initial = 0 
		},
	},
	
    execute = function(dz, item)
		local UV = dz.devices('UV Accuweather')
		local VarPimIvo = dz.variables('Var Luik Pim Ivo')
		local VarOns = dz.variables('Var Luik Ons')
		
		if item == UV and dz.utils.round(UV.uv, 1)  == dz.data.UV then
			dz.log('UV updated but not changed; skip this one ', dz.LOG_DEBUG)
			return
		elseif item == UV then
			dz.data.UV = dz.utils.round(UV.uv, 1)
		end
		
		if  dz.time.matchesRule('at 09:00') or ( UV.uv  < 2 and dz.time.matchesRule('at 09:00-19:00') ) then
			dz.devices('Gordijn Ivo').switchOff()
			dz.devices('Gordijn Pim').switchOff()

		elseif  dz.time.matchesRule('at 09:00') and dz.time.matchesRule('on mon, tue, wed, thu, fri') then
			dz.devices('Gordijn Ons').switchOff()

		elseif  dz.time.matchesRule('at 12:00-18:00') and UV.uv < 2 then
			dz.devices('Gordijn Ons').switchOff()

		elseif  dz.time.matchesRule('at 12:00-18:00') and  
			UV.uv > 1 and (
			dz.devices('Raam Ivo').state == 'Open' or	    
			dz.devices('Raam Pim').state == 'Open' ) then 
			dz.devices('Gordijn Ivo').stop()
			dz.devices('Gordijn Pim').stop()

		elseif  dz.time.matchesRule('at 12:00-19:00') and  
			UV.uv > 1 and (
			dz.devices('Raam Ons Rechts').state == 'Open' or	    
			dz.devices('Raam Ons Links').state == 'Open' ) then 
			dz.devices('Gordijn Ons').stop()

		elseif  dz.time.matchesRule('at 12:00-18:00') and  
			UV.uv > 1 and 
			dz.devices('Raam Ivo').state == 'Closed' and 
			dz.devices('Raam Pim').state == 'Closed' then 
			dz.devices('Gordijn Ivo').switchOn()
			dz.devices('Gordijn Pim').switchOn()

		elseif  dz.time.matchesRule('at 12:00-19:00') and  
			UV.uv > 1 and
			dz.devices('Raam Ons Rechts').state == 'Closed' and	    
			dz.devices('Raam Ons Links').state == 'Closed' then 
			dz.devices('Gordijn Ons').switchOn()

		elseif  dz.time.matchesRule('at 19:01') then
			dz.devices('Gordijn Ivo').switchOn()
			dz.devices('Gordijn Pim').switchOn()

		elseif  dz.time.matchesRule('at 21:00') then
			dz.devices('Gordijn Ons').switchOn()
			end		
		end
	}

Re: Start script when UV changes

Posted: Monday 27 July 2020 20:19
by wouterlet
waaren wrote: Saturday 25 July 2020 14:50
wouterlet wrote: Saturday 25 July 2020 14:27 My problem at the moment is I would like the script to start when the UV from Accuweather changes.
The script will be triggered at the given times and on every update of the given devices. Regardless if the values changed or not.
You need to store the previous value of the UV sensor somewhere to prevent the rest of the script from executing when the UV value did not change. You can use dzVents persistent data for that. It then could look like below.

Code: Select all

return 
{
	on = 
	{
		timer = 
		{	'at 9:00', 
			'at 12:00', 
			'at 19:01', 
			'at 21:00',
		},
		
		devices = 
		{
			'UV Accuweather', 
			'Raam Ons Links', 
			'Raam Ons Rechts', 
			'Raam Ivo', 
			'Raam Pim',
		}
	},
	
	logging = 
	{
		level = domoticz.LOG_DEBUG,
		marker = 'Bedroom shutters', 
	},
	
	data =
	{
		UV = 
		{ 
			initial = 0 
		},
	},
	
    execute = function(dz, item)
		local UV = dz.devices('UV Accuweather')
		local VarPimIvo = dz.variables('Var Luik Pim Ivo')
		local VarOns = dz.variables('Var Luik Ons')
		
		if item == UV and dz.utils.round(UV.uv, 1)  == dz.data.UV then
			dz.log('UV updated but not changed; skip this one ', dz.LOG_DEBUG)
			return
		elseif item == UV then
			dz.data.UV = dz.utils.round(UV.uv, 1)
		end
		
		if  dz.time.matchesRule('at 09:00') or ( UV.uv  < 2 and dz.time.matchesRule('at 09:00-19:00') ) then
			dz.devices('Gordijn Ivo').switchOff()
			dz.devices('Gordijn Pim').switchOff()

		elseif  dz.time.matchesRule('at 09:00') and dz.time.matchesRule('on mon, tue, wed, thu, fri') then
			dz.devices('Gordijn Ons').switchOff()

		elseif  dz.time.matchesRule('at 12:00-18:00') and UV.uv < 2 then
			dz.devices('Gordijn Ons').switchOff()

		elseif  dz.time.matchesRule('at 12:00-18:00') and  
			UV.uv > 1 and (
			dz.devices('Raam Ivo').state == 'Open' or	    
			dz.devices('Raam Pim').state == 'Open' ) then 
			dz.devices('Gordijn Ivo').stop()
			dz.devices('Gordijn Pim').stop()

		elseif  dz.time.matchesRule('at 12:00-19:00') and  
			UV.uv > 1 and (
			dz.devices('Raam Ons Rechts').state == 'Open' or	    
			dz.devices('Raam Ons Links').state == 'Open' ) then 
			dz.devices('Gordijn Ons').stop()

		elseif  dz.time.matchesRule('at 12:00-18:00') and  
			UV.uv > 1 and 
			dz.devices('Raam Ivo').state == 'Closed' and 
			dz.devices('Raam Pim').state == 'Closed' then 
			dz.devices('Gordijn Ivo').switchOn()
			dz.devices('Gordijn Pim').switchOn()

		elseif  dz.time.matchesRule('at 12:00-19:00') and  
			UV.uv > 1 and
			dz.devices('Raam Ons Rechts').state == 'Closed' and	    
			dz.devices('Raam Ons Links').state == 'Closed' then 
			dz.devices('Gordijn Ons').switchOn()

		elseif  dz.time.matchesRule('at 19:01') then
			dz.devices('Gordijn Ivo').switchOn()
			dz.devices('Gordijn Pim').switchOn()

		elseif  dz.time.matchesRule('at 21:00') then
			dz.devices('Gordijn Ons').switchOn()
			end		
		end
	}
Thanks for your answer. If I understand you right the script should allready run when the UV changes? Becuase of device UV Accuweather in the top? I had the feeling that the script didn't run because sometimes the screens react late. I'll give it a try with your script and see how it works.
I can see allready that your part in the script runs because he gives:

020-07-27 20:10:18.620 Status: dzVents: Info: Handling events for: "UV Accuweather", value: "0.0;0.0"
2020-07-27 20:10:18.620 Status: dzVents: Info: Bedroom shutters: ------ Start internal script: Gordijnen Boven DzVents test: Device: "UV Accuweather (AccuWeather)", Index: 319
2020-07-27 20:10:18.621 Status: dzVents: Debug: Bedroom shutters: UV updated but not changed; skip this one
2020-07-27 20:10:18.622 Status: dzVents: Info: ------ Start internal script: Screens DzVents: Device: "UV Accuweather (AccuWeather)", Index: 319

But this seems at random times? When should this run do you think? Or is this random indeed? I can't seem to understand that from your script. Never used persistent data before.

Re: Start script when UV changes

Posted: Monday 27 July 2020 21:11
by waaren
wouterlet wrote: Monday 27 July 2020 20:19 If I understand you right the script should allready run when the UV changes? Becuase of device UV Accuweather in the top?
Yes

But this seems at random times? When should this run do you think?
It runs at 9:00, at 12:00, at 19:01, at 21:00 and every time any of the devices UV Accuweather, Raam Ons Links, Raam Ons Rechts, Raam Ivo or Raam Pim is updated.

Re: Start script when UV changes

Posted: Saturday 01 August 2020 15:30
by wouterlet
hmm, I think I know what goes wrong. On an UV change he only does 1 elseif.
I noticed that sometimes the shutters from "Pim and Ivo" where set to the right position and shutters "Ons" not. And sometimes the other way around.
When I then open or close a window the script goes again and the other shutters also goes in the right position.

Am I right that dzvents stops the script if 1 elseif is right? Or am I wrong in this?

Re: Start script when UV changes

Posted: Saturday 01 August 2020 16:28
by waaren
wouterlet wrote: Saturday 01 August 2020 15:30 Am I right that dzvents stops the script if 1 elseif is right? Or am I wrong in this?
If you mean skipping the other elseif lines; Yes. Lua (dzVents = Lua) and any other programming language work like this. See below. The code walks lineair trough the if else block and as soon as a condition evaluates to true the actions after it until the next else / elseif line are executed and then the code jumps to the first line after the next end.

Code: Select all

-- if else block
if condition1 is true then
    do stuff1
elseif condition2 is true then
    do stuff2
elseif condition3 is true then
    do stuff3
else
   do stuff4
end
-- end of if else block 

--start of next block

If you want the code to check multiple conditions regardless if the previous one is true or not you should code it like

Code: Select all

if condition1 is true then
    do stuff1
end
if condition2 is true then
    do stuff2
end
if condition3 is true then
    do stuff3
end

Re: Start script when UV changes  [Solved]

Posted: Monday 03 August 2020 23:57
by wouterlet
Ah That will be my mistake, thanks!