Page 1 of 1

Device Array Help.

Posted: Wednesday 17 January 2018 5:05
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

Re: Device Array Help.

Posted: Wednesday 17 January 2018 9:36
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.

Re: Device Array Help.

Posted: Friday 19 January 2018 12:04
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


Re: Device Array Help.

Posted: Monday 22 January 2018 6:25
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

Re: Device Array Help.

Posted: Monday 22 January 2018 7:07
by dannybloe
So setState(‘open’) or something like that doesn’t work?

Re: Device Array Help.

Posted: Monday 22 January 2018 9:30
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


Re: Device Array Help.

Posted: Monday 22 January 2018 9:55
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'.

Re: Device Array Help.

Posted: Monday 22 January 2018 11:16
by Wob76
Great, thanks for testing, I must have had somethingelse messing with my setState commands.

Sent from my SM-G935F using Tapatalk