Device Array Help.

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

Moderator: leecollings

Post Reply
Wob76
Posts: 110
Joined: Wednesday 19 April 2017 6:31
Target OS: Linux
Domoticz version:
Contact:

Device Array Help.

Post by Wob76 »

Hi,

I have a script that checks Doors and Windows and send a message if they are open and the Air Con is on, This works fine, and to date I have just checked the outside openings.

I want to merge two devices, as I am only concerned if both devices are open, if the internal door is closed then the air can't reach the open window.

So, I thought I could just build a dummy device, and check that, but as my function calls the domoticz.device it doesn't like that, just after an easy way to do this without rewriting my function.

my previous array of devices looks like this.

Code: Select all

        local openings = {
            [FRONTDOOR_DEVICE]=true,
            [BACKDOOR_DEVICE]=true,
            [LAUNDRYWINDOW_DEVICE]=true,
            [PLAYROOMWINDOW_DEVICE]=true
        }
my function looks like this

Code: Select all

            domoticz.devices().filter( function(device)
                return (openings[device.name] == true)
            end).forEach( function(device)
                if (device.state == "Open") then
                    if LOGGING then domoticz.log("Turning ON the Openings Light as "..device.name.." is "..device.state, domoticz.LOG_FORCE) end
                    domoticz.setScene('Yellow Light', 'On')
                    domoticz.devices(AC_DOORLIGHT).switchOn().checkFirst()
                    open_count = open_count + 1
                end
            end)
So I want to replace PLAYROOMWINDOW_DEVICE with a dummy playroom device that is only "Open" when both the PLAYROOMWINDOW_DEVICE and a new PLAYROOMDOOR_DEVICE are both open, My attempt to create a dummy device looked like this

Code: Select all

        local playroom = {
            name = "Playroom Window and Door"
        }
        if (domoticz.devices(PLAYROOMWINDOW_DEVICE).state == "Open" and domoticz.devices(PLAYROOMDOOR_DEVICE).state == "Open") then
                playroom.state = "Open"
        else
                playroom.state = "Closed"
        end
I added that to the openings array without success. Is this even possible, or should I be looking at an alternative route.

Thanks,
Wob
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Device Array Help.

Post by dannybloe »

So, without looking too deep into your code I'd do it with a dummy switch:

Code: Select all

return {
	on = {
		devices = { 'door', 'window' }
	},
	execute = function(dz, item)
		if (dz.devices('door').active or dz.devices('window').active) then
			dz.devices('openEntries').switchOn().checkFirst() -- update dummy switch
		else
			dz.devices('openEntries').switchOn().checkFirst()
		end	
	end
}
Just to give you an idea. You can put this all in one script but of course.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
Wob76
Posts: 110
Joined: Wednesday 19 April 2017 6:31
Target OS: Linux
Domoticz version:
Contact:

Re: Device Array Help.

Post by Wob76 »

dannybloe wrote:So, without looking too deep into your code I'd do it with a dummy switch:

Code: Select all

return {
	on = {
		devices = { 'door', 'window' }
	},
	execute = function(dz, item)
		if (dz.devices('door').active or dz.devices('window').active) then
			dz.devices('openEntries').switchOn().checkFirst() -- update dummy switch
		else
			dz.devices('openEntries').switchOn().checkFirst()
		end	
	end
}
Just to give you an idea. You can put this all in one script but of course.
Ahh OK, so my best option is to create the dummy switch in Domoticz and action that. Don't know why I didn't think of doing that, I was just trying to create an array object in the script.

Thanks for the help.

Sent from my SM-G935F using Tapatalk

Wob76
Posts: 110
Joined: Wednesday 19 April 2017 6:31
Target OS: Linux
Domoticz version:
Contact:

Re: Device Array Help.

Post by Wob76 »

Thanks @dannybloe, It worked, I did have to separate this function from my other script as it would only see the Dummy devices state before the script ran, and not after the merging function ran. I have another script doing lights in that room, using the door contact, so I merged it with that.

Also just a note for anyone else finding this thread, the above logic needed to be an "and" rather than an "or" for the device check, as I only want my dummy device open if both door and windows are open. Also dannybloe has a .switchOn() for both functions (typo I am sure), in my case the second (else) needs to be a .switchOff()

I used a Door Contact as my dummy switch, so it reports Open\Closed like the other devices, I can still switch on\off these devices with dzVents, interestingly, I can't switch a contact device (usually used for anything other than a door), I even tried .setState() without success. Not a big issue for me, I plan on hidding this device anyway, but thought it interesting that one contact type works and the other does not.

Wob
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Device Array Help.

Post by dannybloe »

So setState(‘open’) or something like that doesn’t work?
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
Wob76
Posts: 110
Joined: Wednesday 19 April 2017 6:31
Target OS: Linux
Domoticz version:
Contact:

Re: Device Array Help.

Post by Wob76 »

dannybloe wrote:So setState(‘open’) or something like that doesn’t work?
I had setSate("Open") and setState("Closed") as it's always uppercase in responses, also tried "On" and "Off" as that's what the device logs show for contact.

I'll test again when I get a chance, just to make sure I didn't have a fault in my test logic, but I'm fairly confident it was right.

Sent from my SM-G935F using Tapatalk

dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Device Array Help.

Post by dannybloe »

I just tested it here and if you do myDevice.close() or myDevice.open() it should work. What it does though is it sends an On/Off command to Domoticz. So you can also do: myDevice.setState('On') or 'Off'.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
Wob76
Posts: 110
Joined: Wednesday 19 April 2017 6:31
Target OS: Linux
Domoticz version:
Contact:

Re: Device Array Help.

Post by Wob76 »

Great, thanks for testing, I must have had somethingelse messing with my setState commands.

Sent from my SM-G935F using Tapatalk

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest