Definitions

Message

def fields = [:]

fields["status"] = "INTEGER";
fields["flow_rate"] = "FLOAT";
fields["total_consumption"] = "FLOAT";

domain.defineMessage("Flow Meter Message", 10001, fields);

Record

def fields = [:]

fields["created_ts"] = "TIMESTAMP";
fields["device_id"] = "TEXT";
fields["event_name"] = "TEXT";
fields["flow_rate"] = "FLOAT";

domain.defineMessage("Flow Meter Events", 10001, fields);

Message Rule

FLOW_REC_ID = 10001;
def data = [:];

HIGH_FLOW_THRESHOLD = 40.0;
LOW_FLOW_THRESHOLD = 20.0;

data['device_id'] = msg.deviceid;
data['created_ts'] = util.millis();
data['flow_rate'] = msg.flow_rate;

log.info("message%s",msg);

if(msg.flow_rate >= HIGH_FLOW_THRESHOLD){
    
    log.info("High Flow");
    data['event_name'] = 'HIGH';
    record.insert(FLOW_REC_ID,data);
}
else if(msg.flow_rate <= LOW_FLOW_THRESHOLD){
       
    log.info("Low Flow");
    data['event_name'] = 'LOW'
    record.insert(FLOW_REC_ID,data);

}
else {
    log.info("Normal Flow")
}