Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add updateTime to meta topic for device with battery #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

SergeSpinoza
Copy link

@SergeSpinoza SergeSpinoza commented Feb 3, 2024

Added information about the last data sent - updateTime - to the meta topic for devices with a battery. Because It is possible that the connection with the device will be lost and we will not know about it in wirenboard. This problem is especially critical, for example, for leakage sensors.

Example of use in wb-rules:

var zwaveSensors = [
  {
    'mqttTopicBattery': '/devices/zway-2/controls/Battery (2) 2-0-128/meta/updatetime',
    'title': 'Z-Wave. Leak Detector, id 2', 
    'updateTime': 0,
    'wasOnline': true
  }
];


// Consider devices offline if the last battery status update was greater than offlineUpdateTime seconds ago
var offlineUpdateTime = 5400; // 1 hour 30 minutes

// Subscribe to mqtt topic with updatetime
for (sensor in zwaveSensors) {
  trackMqtt(zwaveSensors[sensor].mqttTopicBattery, function(message){
    zwaveSensors[sensor].updateTime = message.value;
  });
}

// Logging and alerting functions
function deviceOnlineReport (sensor) {
  log("Device: {}, Topic: {}, device is returned Online!", sensor.title, sensor.mqttTopicBattery);
}

function deviceOfflineReport (sensor, delay) {
  log("Device: {}, Topic: {}, the device did not respond for more than {} minutes", sensor.title, sensor.mqttTopicBattery, delay);
}

function deviceDailyOfflineReport (sensor) {
  log("Device: {}, Topic: {}, device is Offline.", sensor.title, sensor.mqttTopicBattery);
}


// Checking the last data sent from the device
defineRule("sensor_zwave_check_status", {
  when: cron("00 00 */1 * *"),
  then: function () {
    var dateNow = Math.floor(Date.now() / 1000); 

    for (sensor in zwaveSensors) {
      var updateTime = zwaveSensors[sensor].updateTime;
      
      if (updateTime < 0) {
        log.error("Error reading mqtt topic {}", zwaveSensors[sensor].mqttTopicBattery);
        continue;
      }
      
      var delay = dateNow - zwaveSensors[sensor].updateTime;
      var isOnline = delay < offlineUpdateTime;
      
      if (isOnline != zwaveSensors[sensor].wasOnline) {
          isOnline
          ? deviceOnlineReport(zwaveSensors[sensor])
          : deviceOfflineReport(zwaveSensors[sensor],Math.floor(delay/60));
      }
    
      zwaveSensors[sensor].wasOnline = isOnline;
    }
  }
});

// Reminder once a day that the device is not connected
defineRule("sensor_zwave_offline", {
  when: cron("00 05 12 * *"),
  then: function () {
    for (sensor in zwaveSensors) {
      if (zwaveSensors[sensor].wasOnline === false) {
        deviceDailyOfflineReport(zwaveSensors[sensor]);
      }
    }
  }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant