Page 1 of 1

Multiple choice selector switch

Posted: Wednesday 04 December 2019 23:40
by James83
Hi,

I have some selector switches by wich events will be executed or not, depending on its state.
Sometimes, multiple states may be executed but then i need to combine levels in new levels, like: level10= x, level20= y, level30= z, level40= x+y, level50= x+y+z, level60= y+z, level70= x+z. In my dzVents scripts i have to use string.match on the selector switch.state, wich does not always work witch symbols like ":" and "()".
Therefore it would be usefull if i could just enable the levels that need to be executed and then use somthing like <multiselectorswitch.level('x').active == true> in my scripts. I would also need less levels in my selector switch.

Or does something similar already exists?

Thx,
Jimmy

Re: Multiple choice selector switch

Posted: Thursday 05 December 2019 0:58
by waaren
James83 wrote: Wednesday 04 December 2019 23:40 I have some selector switches by wich events will be executed or not, depending on its state. Sometimes, multiple states may be executed but then i need to combine levels in new levels
Would indeed be nice to have such a device but unfortunately it does not exist in domoticz and if I understand the underlying code for devices correctly, I don't think it will be very easy to implement either. In other words: don't hold your breath until it is implemented.
Until then I am happy to see if I can help to overcome the problem with string.match and identify which states are active when combined.

Re: Multiple choice selector switch

Posted: Sunday 22 December 2019 9:56
by James83
waaren wrote: Thursday 05 December 2019 0:58 Until then I am happy to see if I can help to overcome the problem with string.match and identify which states are active when combined.
Thanks for your fast reply and sorry for my late answer :-)

I worked arround this by renaming my devices, so i don't need string.match anymore in this case. But maybe for your interest, here's what went wrong:

Deur Kelder MK => triggered device.name
Deur Kelder MK => selectorswitch.state
Deur Kelder MK => output of string.match(selectorswtich.state,device.name)

Deur Kelder (MK) => triggered device.name
Deur Kelder (MK) => selectorswitch.state
nil => output of string.match(selectorswtich.state,device.name)

So, the "()" in the name of a device (or maybe any string) always output "NIL" with string.match.
Similar issue when using wildcards in the on-section. If i use for exemple: on = *(MK)*, other devices with "MK" (without the -()-) also get triggered.

Another issue with symbols i ran into, are the ":". Dazzled me for hours :D
A device holding ":" in it's name, doesn't get switched, no errors logged. I removed the ":" in the devicename and the problem was solved.

In the beginning, i used Blockly for these few scripts and they worked fine, but since they were getting to complicated i wrote them in dzVents. Therefore, i could not understand why it would not work.
Now i know i have to avoid using symbols in names ;)

Thx,
Jimmy

Re: Multiple choice selector switch

Posted: Sunday 22 December 2019 12:37
by waaren
James83 wrote: Sunday 22 December 2019 9:56 Deur Kelder (MK) => triggered device.name
Deur Kelder (MK) => selectorswitch.state
nil => output of string.match(selectorswtich.state,device.name)
So, the "()" in any string) always output nil with string.match.
Thx for getting back to me and reporting what caused some of your issues. I need to investigate the unexpected behavior in more detail but if I can modify dzVents to make it more reliable for this I will.

The () are so called magic chars in Lua pattern matching and that's why your match result is not what you expected. One way to work around this is the use of a 'sanitized' string.match function. (see below)

Code: Select all

local function string.sMatch(text, match) -- add sanitized match function to string "library"
    local sanitizedMatch = match:gsub("([%%%^%$%(%)%.%[%]%*%+%-%?])", "%%%1") -- escaping all 'magic' chars
    return text:match(sanitizedMatch)
end

print(string.sMatch(selectorswitch.state,device.name))
Deur Kelder (MK)

-- or 

print(selectorswitch.state:sMatch(device.name))
Deur Kelder (MK)