ariela
(Andrea Riela)
February 9, 2019, 1:28pm
1
Hi folks,
My VSC is highlighting this in my rule:
The method or field members is undefined for the type Iterable<String>
The method or field str is undefined
Here the switch statement in the rule is referring to:
switch(cameHome.size) {
case 0: {
logError("came home", "Unexpected number of phones: " + cameHome.size)
return;
}
case cameHome.size == 1 && cameHome.get(0) == "Andrea": message = "Benvenuto a casa, Andrea"
case cameHome.size == 1 && cameHome.get(0) == "Cate": message = "Benvenuta a casa, Caterina"
case cameHome.size == 1 && cameHome.get(0) == "Sofia": message = "Benvenuta a casa, Sofia"
case cameHome.size == 1 && cameHome.get(0) == "Edo": message = "Benvenuto a casa, Edoardo"
default: {
message = message + cameHome.members.reduce[ String list, String name | list + ", " + name ]
val ind = message.lastIndexOf(",")
message = new StringBuilder(str).replace(ind, ind+1," e").toString();
}
}
Any suggestion how to fix it?
Thanks
Andrea
vzorglub
(Vincent Regaud)
February 9, 2019, 1:58pm
2
ariela:
cameHome
What is cameHome
, an item? a String?
ariela
(Andrea Riela)
February 9, 2019, 2:03pm
3
here the full rule:
// Presence (Home) Unifi in
rule "Welcome home my dear"
when
Member of gPhones changed to ON
then
if(cameHomeTimer !== null) {
return; // do nothing if there is already a Timer
}
logInfo("test", "Rule triggered by " + triggeringItem.name + " at " + now.toString)
cameHomeTimer = createTimer(now.plusSeconds(5), [ |
val cameHome = gPhones.members.filter[ p | p.state == ON && (p.name == triggeringItem.name || (p.lastUpdate !== null && p.lastUpdate.isAfter(now.minusSeconds(6)))) ].map[ name.replace("_Phone_Home_Unifi_online", "") ]
gPhones.members.forEach[ p | logInfo("test", "Values for " + p.name + "\n" + "State = " + p.state.toString + "\n" + "LastUpdate = " + p.lastUpdate.toString)]
var message = "Benvenuti a casa, "
switch(cameHome.size) {
case 0: {
logError("came home", "Unexpected number of phones: " + cameHome.size)
return;
}
case cameHome.size == 1 && cameHome.get(0) == "Andrea": message = "Benvenuto a casa, Andrea"
case cameHome.size == 1 && cameHome.get(0) == "Cate": message = "Benvenuta a casa, Caterina"
case cameHome.size == 1 && cameHome.get(0) == "Sofia": message = "Benvenuta a casa, Sofia"
case cameHome.size == 1 && cameHome.get(0) == "Edo": message = "Benvenuto a casa, Edoardo"
default: {
message = message + cameHome.members.reduce[ String list, String name | list + ", " + name ]
val ind = message.lastIndexOf(",")
message = new StringBuilder(str).replace(ind, ind+1," e").toString();
}
}
logInfo("check message", "message is: " + message)
Echo_Living_Room_TTS_Volume.sendCommand('90')
//Echo_Living_Room_TTS.sendCommand('<speak><break time="2s"/>' + message + '</speak>')
Echo_Living_Room_TTS.sendCommand(message)
cameHomeTimer = null
])
end
vzorglub
(Vincent Regaud)
February 9, 2019, 2:07pm
4
cameHome is not a group no members will not work, remove it.
message = message + cameHome.reduce[ String list, String name | list + ", " + name ]
The second error is due to str
not being defined
What is it?
ariela
(Andrea Riela)
February 9, 2019, 2:09pm
5
is referring to this piece of rule at the end, in order to create a message with people joining the wifi network “at the same time” (in cameHomeTimer):
vzorglub
(Vincent Regaud)
February 9, 2019, 2:14pm
6
Of the stringBuilder doesn’t quite work like that, see:
Please see Design Pattern: What is a Design Pattern and How Do I Use Them for how to read and use DPs.
Problem Statement
Often one finds a number of Rules that are very similar and that all work on similar Items resulting in a lot of duplicated code. One way to solve this problem is through the use of lambdas but an even more flexible approach is to use Groups.
Concept
[image]
This DP is not a standard DP in that if provides a single concrete template to follow to solve a specific problem. In…
ariela
(Andrea Riela)
February 9, 2019, 2:41pm
7
Let me see if I understood correctly.
var message = "Benvenuti a casa, "
switch ...
default: {
message = message + cameHome.reduce[ String list, String name | list + ", " + name ]
val ind = message.lastIndexOf(",")
message = ......
}
message by default will be "Benvenuti a casa, " plus:
the first default line defines a list of people, eg Andrea, Cate, …
the second line check the latest “,”
third line should replace the latest “,” with “and” (“e” in italian)
I will work on the third line …
ariela
(Andrea Riela)
February 9, 2019, 2:57pm
8
what do you think?
default: {
message = message + cameHome.reduce[ String list, String name | list + ", " + name ]
val ind = message.lastIndexOf(",").toString
message = message.replace(ind, " e ").toString();
}
vzorglub
(Vincent Regaud)
February 9, 2019, 3:11pm
9
Looks ok, don’t forget to declare and initialise the variable:
rule "Welcome home my dear"
when
Member of gPhones changed to ON
then
var String message = ""
if(cameHomeTimer !== null) {
ariela
(Andrea Riela)
February 9, 2019, 3:16pm
10
Yep but inside the timer expression:
cameHomeTimer = createTimer(now.plusSeconds(5), [ |
val cameHome = gPhones.members.filter[ p | p.state == ON && (p.name == triggeringItem.name || (p.lastUpdate !== null && p.lastUpdate.isAfter(now.minusSeconds(6)))) ].map[ name.replace("_Phone_Home_Unifi_online", "") ]
gPhones.members.forEach[ p | logInfo("test", "Values for " + p.name + "\n" + "State = " + p.state.toString + "\n" + "LastUpdate = " + p.lastUpdate.toString)]
var String message = "Benvenuti a casa: "
switch(cameHome.size) {
case 0: {