[SOLVED] VSC errors in rule

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

What is cameHome, an item? a String?

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

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?

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):

Of the stringBuilder doesn’t quite work like that, see:

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 …

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();
            }

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) {

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: {