[SOLVED] Monitor multiple Item changes in a single rule

I think I know what the unfortunate answer will be, but wanted to validate first. I currently have the following rule setup that is set to create a simple notification when someone is home or leaves.

rule "Location Tracking"
when
  Item virtual_user1 changed or
  Item virtual_user2 changed
then
  if(virtual_user1.state == ON){
    pushover("user1 is home")
  }
  else if(virtual_user1.state == OFF){
    pushover("user1 has left")
  }
  else if(virtual_user2.state == ON){
    pushover("user2 is home")
  }
  else{
    pushover("user2 has left")
  }
end

The problem, as I thought through it, is that no matter what this state will always meet 1 of 2 first evaluations. I can separate this into 2 separate rules, but what I’m looking for is a way to simplify this and keep it as a single rule instead.

EDIT: I think my optimal mechanism would be to be able to evaluate WHICH of my WHEN statements was triggered. If I could use that for evaluation, this becomes much easier, but I’m not sure that is possible or if so, how to go about doing so.

If you are only looking to track the state of individual persons, then the simplest (and IMO cleanest) approach is to separate what you have into two rules - one for each person.

Trying to implement this in one rule - if your goal is to only send a notification when the state of one person changes - is likely to be messy.

Thank you, I had a feeling that was likely the case. There may be more complicated logic that get’s built off of this later on (involving alarms, garage doors, etc), but for now I want to validate that the location monitoring is working properly before we start unlocking things, opening things, etc.

Going step-by-step is always a good idea, :slight_smile:

When you start adding automatic actions to this, I presume you want to have some kind of “master presence” switch instead of working with individaul users?

In that case, you will need a rule with trigger conditions like you have above, e.g. something like this:

rule "Master Presence"
when
  Item virtual_user1 changed or
  Item virtual_user2 changed
then
  if (virtual_user1.state == ON) or (virtual_user2.state == ON) {
    // Someone is home
    if (Presence.state != ON) {
      Presence.sendCommand(ON)
    }
  }
  else {
    // Everyone is out
    if (Presence.state != OFF) {
      Presence.sendCommand(OFF)
    }
  }
end

Yes, this is actually very similar, though it accounted for the other 2 options of one person being gone. But this time I wanted to just get notifications for now so I can validate how well the location function is working. Come to find out in fact we have a fun snafu to deal with, in that we live by the highway - so yesterday the other half went driving by and it showed home, then gone. These are the gotchas I have to figure out handling before I turn it on full bore.

For now I’ll stick with the two rules so I know it’s working positively vs guessing and playing with a rule just to get some reconnaissance done. I know I could use logs as well, but the downside is I need an actual idea of when it’s happening vs trying to backtrack and retrace steps where we were.

I appreciate the help, think this is good for now. I’ll mark solved.

Final solution for anyone is below. Solution was to unfortunately split into two separate rules. Bottom line, we can not evaluate 2 when statements and only evaluate against the one that has changed. Alternative option would likely be using timestamp comparisons for current time vs most recent change (some basic math of times), but that would require MUCH more complicated scripting. This is the simplest way forward.

rule "Location Tracking user1"
when
  Item virtual_user1 changed
then
  if(virtual_user1.state == ON){
    pushover("user1 is home")
  }
  else{
    pushover("user1 has left")
  }
end

rule "Location Tracking user2"
when
  Item virtual_user2 changed
then
  if(virtual_user2.state == ON){
    pushover("user2 is home")
  }
  else{
    pushover("user2 has left")
  }
end