I think you just need to add single quotes around your SM_C.key in your eval. This run anywhere search shows the concept working:
| makeresults
| append [| makeresults | eval sensor="sensor1", _time=1000, SM_C.key="RoomMonitor.Occupied"]
| append [| makeresults | eval sensor="sensor2", _time=995, SM_C.key="RoomMonitor.Occupied"]
| append [| makeresults | eval sensor="sensor1", _time=993, SM_C.key="RoomMonitor.UnOccupied"]
| append [| makeresults | eval sensor="sensor2", _time=993, SM_C.key="RoomMonitor.Occupied"]
| streamstats count(eval('SM_C.key'="RoomMonitor.Occupied")) AS occupied_count, count(eval('SM_C.key'="RoomMonitor.UnOccupied")) AS unoccupied_count BY sensor
| eval current_occupied_diff=occupied_count-unoccupied_count
But keep in mind, also, that streamstats considers events above it (those already returned by the search), which are typically in reverse chronological order. This needs to be considered when you run your streamstats , you may need to reverse things first (but that may be a bad idea if there is a lot of data.
... View more