Proteus sum pump level monitor - json

/**

  • ================ Proteus Sump Pump Montitor ===============
  • Driver
  • Platform: Hubitat
  • =======================================================================================
  • Last modified: 2021-04-04
    */

import groovy.transform.Field

@Field static final String protocol = "http"
@Field static final String resourcePath = "/status.json"

metadata {
definition (name: "Proteus Sump Pump Monitor", namespace: "RMoRobert", author: "Robert Morris") {
capability "Sensor"
capability "Initialize"
capability "WaterSensor"
capability "Refresh"
}

preferences() {
input name: "ipAddress", type: "string", title: "Proteus IP address", description: "Example: 192.168.1.2"
input name: "refreshInterval", type: "enum", title: "Refresh every...",
options: ["1 minute", "5 minutes", "10 minutes", "15 minutes", "30 minutes", "1 hour", "3 hours"],
defaultValue: "1 minute"
input name: "enableDebug", type: "bool", title: "Enable debug logging", defaultValue: true
input name: "enableDesc", type: "bool", title: "Enable descriptionText logging", defaultValue: true
}
}

void debugOff() {
log.warn "Disabling debug logging"
device.updateSetting("enableDebug", [value:"false", type:"bool"])
}

void installed() {
log.debug "Installed..."
initialize()
}

void updated() {
log.debug "Updated..."
initialize()
}

void initialize() {
log.debug "Initializing"
unschedule()
if (enableDebug) {
Integer disableTime = 1800
log.debug "Debug logging will be automatically disabled in ${disableTime} seconds"
runIn(disableTime, debugOff)
}
if (enableDebug) log.debug "Configuring polling every ${settings['runEvery'] ?: '1 minute'}..."
switch (settings["runEvery"] ?: "1 minute") {
case "1 minute":
runEvery1Minute("refresh"); break
case "5 minutes":
runEvery5Minutes("refresh"); break
case "10 minutes":
runEvery10Minutes("refresh"); break
case "15 minutes":
runEvery15Minutes("refresh"); break
case "30 minutes":
runEvery30Minutes("refresh"); break
case "1 hour":
runEvery1Hour("refresh"); break
case "3 hours":
runEvery3Hours("refresh"); break
default:
runEvery30Minutes("refresh")
}
if (now() - (device.currentValue("lastUpdated") ?: 0) > 600000) {
if (enableDebug) log.debug "Refreshing since has been longer than 10 minutes..."
runIn(2, "refresh")
}
}

void parse(String description) {
log.warn "Unexpected parse(): $description"
}

void refresh() {
if (enableDebug) log.debug "refresh()"
String uri = "${protocol}://${settings['ipAddress']}${resourcePath}"
if (enableDebug) log.debug "uri = $uri"
Map params = [
uri: uri,
contentType: "application/json",
timeout: 20
]
try {
asynchttpGet("parseRefreshResponse", params)
}
catch (Exception ex) {
log.error "Error in refresh(): $ex"
}
}

void parseRefreshResponse(response, data) {
if (enableDebug) log.debug "parseRefreshResponse()"
if (response.hasError()) {
log.warn(response.getErrorMessage())
}
else if (response?.status == 200 && response?.json) {
if (enableDebug) "response.json = ${response.json}"
if ((response.json.fld as Integer) == 0) {
doSendEvent("water", "wet")
}
else if ((response.json.fld as Integer) == 1) {
doSendEvent("water", "dry")
}
else {
if (enableDebug) log.warn "Unexpected fld value: ${response.json.fld}"
}
}
else {
log.warn "Unexpeted response: HTTP ${response.status}, body = ${response.body}"
}
}

private void doSendEvent(String eventName, eventValue, String eventUnit=null) {
//if (enableDebug) log.debug "doSendEvent(eventName: $eventName, eventValue: $eventValue, eventUnit: $eventUnit)..."
String descriptionText = "${device.displayName} ${eventName} is ${eventValue}${eventUnit != null ? ' ' + eventUnit : ''}"
if (enableDesc && ("${device.currentValue(eventName)}" != "$eventValue")) log.info descriptionText
if (eventUnit != null) sendEvent(name: eventName, value: eventValue, eventUnit: eventUnit, descriptionText: descriptionText)
else sendEvent(name: eventName, value: eventValue, descriptionText: descriptionText)
}

Thank you very much!

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.