The template would use search as a test to select all binary_sensors containing “_occupancy” in their name. However I would create a Template Sensor that reports the quantity that are on and then use a State Trigger to detect when the sensor’s value changes.
Let me know if you need help composing creating the Template Sensor.
