Help with .changed setting

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

Moderator: leecollings

Post Reply
simon_rb
Posts: 612
Joined: Wednesday 07 August 2013 19:09
Target OS: -
Domoticz version:
Location: UK
Contact:

Help with .changed setting

Post by simon_rb »

Hi,

I am learning the basics to DzVents however it is taking me a while. The first thing I am trying to achieve is this:-

If a switch called Activity Kitchen is switched off then switch off a switch called Kitchen TV. I can get this to work however what I need to do is if the Kitchen TV is switch on and the Activity Kitchen is off then the TV is turned off. I'd like to be able to turn on the Kitchen TV and not have it turn off. I only want it to turn off when Activity Kitchen switch is turned off and not just in an off state.

I did try .changed however not sure if I am doing it correctly.

Many Thanks

Code: Select all

return {
	active = true, 
	
	on = {
		devices = { 'Activity Kitchen',
		 

		},
		},


	execute = function(domoticz, Kitchen)

		local ktv = domoticz.devices('Kitchen TV')
		if (Kitchen.changed == 'Off') then
			ktv.switchOff()
			domoticz.notify('Turn off the Kitchen TV')
		end
	end
}
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Help with .changed setting

Post by dannybloe »

You don't have to use the changed attribute anymore. Actually, when the on-trigger is met and your execute function is called, the device (Activity Kitchen in your case) is always changed. After all, the execute function is only executed when the device was changed.
So, I guess you want to check the state: if (Kitchen.state == 'Off') then.. or if (Kitchen.active) then ...
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
simon_rb
Posts: 612
Joined: Wednesday 07 August 2013 19:09
Target OS: -
Domoticz version:
Location: UK
Contact:

Re: Help with .changed setting

Post by simon_rb »

Thanks for the quick reply. I am still stuck, I have changed the script to this and its just in a loop now. I have read all the documentation and it just confuses me. I am better off having a working script that I can understand.

I don't understand the execute = function(domoticz. Kitchen), what is the Kitchen reference to? Any device with the name kitchen in it?

Code: Select all

return {
	active = true, 
	
	on = {
		devices = { 'Activity Kitchen',
		 

		},
		},


	execute = function(domoticz, Kitchen)

		local ktv = domoticz.devices('Kitchen TV')
		if (Kitchen.state == 'Off') then
			ktv.switchOff()
			domoticz.notify('Turn off the Kitchen TV')
		end
	end
}
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Help with .changed setting

Post by dannybloe »

The second parameter in your execute function is the object that was triggered and matches your trigger-rule ('Activity Kitchen'). So when that device is changed then Kitchen in your function is that device object. If 'Activity Kitchen' is a switch then Kitchen is a switch device.

The only reason causing a loop would be that you have other scripts that also affect the 'Activity Kitchen' device. Make sure you don't have any other rules or actions in place (check your devices in the GUI and make sure there are no other scripts active somehow). If the script you posted above is the only script on your system then it should not cause a loop. Unless you have devices with equal names in your system. Check the logs. You can enable debug logging in the settings to have more information in the log. That might help you.

Perhaps switching "Kitchen TV' triggers 'Activity Kitchen' somehow. That could make it loop.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
simon_rb
Posts: 612
Joined: Wednesday 07 August 2013 19:09
Target OS: -
Domoticz version:
Location: UK
Contact:

Re: Help with .changed setting

Post by simon_rb »

Ok,

Thanks again. So the script above is correct? If I turn on the Kitchen TV then it will stay on even if the Activity Kitchen is off? Once the Activity Kitchen is switched off then the Kitchen TV is switched off.. I will investigate further.

Its definitely the script above thats looping round. Nothing else has an effect on Activity Kitchen.

Just turning off the Kitchen TV even when the Activity Kitchen is off sets the loop going.
simon_rb
Posts: 612
Joined: Wednesday 07 August 2013 19:09
Target OS: -
Domoticz version:
Location: UK
Contact:

Re: Help with .changed setting

Post by simon_rb »

Right, ignore above post. It works fine. A bit of fault finding and found you were right. The Kitchen TV powered off would turn off the Activity Kitchen. I need to do the same for 3 other rooms, can I add them all to the same script? 3 Different Activity Rooms and TV's..
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Help with .changed setting

Post by dannybloe »

Yes, you can add as many triggers in one script as you like. Only thing is that the name of your execute parameter 'Kitchen' is then not appropriate. I'd call it device or item. And.. inside your execute function you can check the .name property in case you need to do specific stuff depending on the device.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
simon_rb
Posts: 612
Joined: Wednesday 07 August 2013 19:09
Target OS: -
Domoticz version:
Location: UK
Contact:

Re: Help with .changed setting

Post by simon_rb »

Ok, good to know - and confused. I think I'm making headway. This is what I have so far:-

Code: Select all

return {
	active = true, 
	
	on = {
		devices = { 'Activity Kitchen', 'Activity', 'Activity Bedroom', 'Activity Dining',
		 

		},
		},


	execute = function(domoticz, Kitchen)

		local ktv = domoticz.devices('Kitchen TV')
		local btv = domoticz.devices('Dining Room - TV')
		local dtv = domoticz.devices('Bedroom - TV')
		local act = domoticz.devices('Activity')
		if (Kitchen.state == 'Off') and (act.state =='Off') then
			ktv.switchOff()
			domoticz.notify('Turned off the Kitchen TV')
		end
	end
}
Now I need to do the same above as for Dining Room and Bedroom. When both the Activity and the Appropriate Room activity have been turned off then turn off the TV in that room.. I'm a bit stuck on how to add anymore to it for the following rooms. Above works for the Kitchen.
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Help with .changed setting

Post by dannybloe »

Well, you may have to check for the specific device that was triggered (in your on-devices list). So I think you should test for them in your execute function. Just to give you an idea:

Code: Select all

return {
	on = {
		devices = { 'Activity Kitchen', 'Activity', 'Activity Bedroom', 'Activity Dining'} -- you may use wild cards here like Activity*
	}
	execute = function(domoticz, item)
		local tv
		local act = domoticz.devices('Activity')
		
		if (item.name == 'Activity Kitchen') then
			tv = domoticz.devices('Kitchen TV')
		end
		
		if (item.name == 'Activity Bedroom') then
			tv = domoticz.devices('Bedroom - TV')
		end
		
		
		if (item.state == 'Off' and act.state == 'Off') then
			tv.switchOff()
			domoticz.notify('Turned off '  .. tv.name)
		end
		
	end
}


Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
simon_rb
Posts: 612
Joined: Wednesday 07 August 2013 19:09
Target OS: -
Domoticz version:
Location: UK
Contact:

Re: Help with .changed setting

Post by simon_rb »

Right, thats great. I think I get it now. So the item would just store the name then you call the item and if that name matches one then item becomes that then can switch them off it it was one of those! Thanks. Just add dining room to the above and it should be pretty much there. Thats amazing.

I watched your 40 odd minute video on youtube which was handy. I think I'll get there. Thanks again dannybloe

Update:- All working great thanks! I'll do more testing and playing about with it. Such a clever little script! Thanks again. Would have never thought to do it that way.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest