Room Occupancy Rule

I am now tracing room occupancy using room-assistant (see I am tracking people using iBeacons. I have put the my wife and I into a group called gKCBeacon (ChetBeacon, KathrinBeacon are members).

I have also created 4 room occupancy switches LivingRoom, Bedroom, office, temple (its actually a gym/meditation room). They are in a group called gRoomOccupancy.

I wrote a script to switch on a room when it was occupied:

rule "Room Occupancy Updater"
    Member of gKCBeacon changed
    if(ChetBeacon.state == "LivingRoom" || KathrinBeacon.state == "LivingRoom") {
        LivingRoom.sendCommand(ON) }else LivingRoom.sendCommand(OFF)
    if(ChetBeacon.state == "office" || KathrinBeacon.state == "office") {
        office.sendCommand(ON)}else office.sendCommand(OFF)
    if(ChetBeacon.state == "Bedroom" || KathrinBeacon.state == "Bedroom") {
        Bedroom.sendCommand(ON) }else Bedroom.sendCommand(OFF)
    if(ChetBeacon.state == "temple" || KathrinBeacon.state == "temple") {
        temple.sendCommand(ON) }else temple.sendCommand(OFF)

The above rule works fine. However, after reading a DP I wanted to upgrade this rule. The result is:

rule "Room Occupancy Updater" //Used information from DP: Associated Items
    Member of gKCBeacon changed
    gRoomOccupancy.members.forEach[ SwitchItem r |
        if (r.state == ON && != ChetBeacon.state || != KathrinBeacon.state) r.sendCommand(OFF)
        if (r.state == OFF && == ChetBeacon.state || == KathrinBeacon.state) r.sendCommand(ON)

The above rule throws no errors, no problems reported in VS Code, but it doesn’t work. The “if” statements are never triggered. I have been staring at this for two days and can’t figure out what’s wrong.

I may have bitten off more than I can chew. Does anyone see an error in the “if” statements, or does the problem lie somewhere else?

  • Platform information:
    • Hardware: Gigabyte miniPC i7
    • OS: _Unbuntu 18.04.04LTS
    • Java Runtime Environment: openjdk version “1.8.0_232”, Zulu
    • openHAB version: openHabian 2.5.3-1 release
  gRoomOccupancy.members.forEach[ r |
        if (r.state == ON && ( != ChetBeacon.state || != KathrinBeacon.state)) r.sendCommand(OFF)
        if (r.state == OFF && ( == ChetBeacon.state || == KathrinBeacon.state)) r.sendCommand(ON)

Does this work any better?

1 Like

You only need to check the state of your beacons once and the name attribute is a String but state is a State…

gRoomOccupancy.members.forEach[ room |
    if ( != ChetBeacon.state.toString || != KathrinBeacon.state.toString) {
        room.sendCommand(if (room.state == OFF) ON else OFF)

However, your hardware would go really well with…

@lfs_alp5 Unfortunately not

@5iver Thanks, the “toString” did the trick! Unfortunately the logic is quite right.
My wife and I are in the LivingRoom. The group state is:
LivingRoom ON
Bedroom OFF
Office OFF
Temple OFF

I incorporated your change, then I used MQTT Explorer to change my location to Bedroom. The rule change the group state to:
LivingRoom OFF
Bedroom ON
Office ON
Temple ON

Its just a matter of getting the logic right now.

I think I understand better what you have going on… try this one…

gRoomOccupancy.members.forEach[ room |
    if ( == ChetBeacon.state.toString || == KathrinBeacon.state.toString) {
    } else {

Thanks 5iver. that did it. I was trying out variations on the “room.sendCommand(if (room.state…” line without any luck. Your solution has everything working correctly.

A thousand thank you’s and a big debt of gratitude to you!

With that solved, I will read the link you provided about Area Triggers


1 Like