mirror of
https://github.com/mtan93/SmartThingsPublic.git
synced 2026-03-08 05:31:56 +00:00
Merge pull request #2196 from varzac/smartsense-battery-smoothing
Smooth battery values for smartsense devices
This commit is contained in:
@@ -173,11 +173,27 @@ private Map getBatteryResult(rawValue) {
|
|||||||
} else {
|
} else {
|
||||||
def minVolts = 2.4
|
def minVolts = 2.4
|
||||||
def maxVolts = 2.7
|
def maxVolts = 2.7
|
||||||
|
// Get the current battery percentage as a multiplier 0 - 1
|
||||||
|
def curValVolts = Integer.parseInt(device.currentState("battery")?.value ?: "100") / 100.0
|
||||||
|
// Find the corresponding voltage from our range
|
||||||
|
curValVolts = curValVolts * (maxVolts - minVolts) + minVolts
|
||||||
|
// Round to the nearest 10th of a volt
|
||||||
|
curValVolts = Math.round(10 * curValVolts) / 10.0
|
||||||
|
// Only update the battery reading if we don't have a last reading,
|
||||||
|
// OR we have received the same reading twice in a row
|
||||||
|
// OR we don't currently have a battery reading
|
||||||
|
// OR the value we just received is at least 2 steps off from the last reported value
|
||||||
|
if(state?.lastVolts == null || state?.lastVolts == volts || device.currentState("battery")?.value == null || Math.abs(curValVolts - volts) > 0.1) {
|
||||||
def pct = (volts - minVolts) / (maxVolts - minVolts)
|
def pct = (volts - minVolts) / (maxVolts - minVolts)
|
||||||
def roundedPct = Math.round(pct * 100)
|
def roundedPct = Math.round(pct * 100)
|
||||||
if (roundedPct <= 0)
|
if (roundedPct <= 0)
|
||||||
roundedPct = 1
|
roundedPct = 1
|
||||||
result.value = Math.min(100, roundedPct)
|
result.value = Math.min(100, roundedPct)
|
||||||
|
} else {
|
||||||
|
// Don't update as we want to smooth the battery values
|
||||||
|
result = null
|
||||||
|
}
|
||||||
|
state.lastVolts = volts
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -275,11 +275,27 @@ private Map getBatteryResult(rawValue) {
|
|||||||
} else {
|
} else {
|
||||||
def minVolts = 2.1
|
def minVolts = 2.1
|
||||||
def maxVolts = 2.7
|
def maxVolts = 2.7
|
||||||
|
// Get the current battery percentage as a multiplier 0 - 1
|
||||||
|
def curValVolts = Integer.parseInt(device.currentState("battery")?.value ?: "100") / 100.0
|
||||||
|
// Find the corresponding voltage from our range
|
||||||
|
curValVolts = curValVolts * (maxVolts - minVolts) + minVolts
|
||||||
|
// Round to the nearest 10th of a volt
|
||||||
|
curValVolts = Math.round(10 * curValVolts) / 10.0
|
||||||
|
// Only update the battery reading if we don't have a last reading,
|
||||||
|
// OR we have received the same reading twice in a row
|
||||||
|
// OR we don't currently have a battery reading
|
||||||
|
// OR the value we just received is at least 2 steps off from the last reported value
|
||||||
|
if(state?.lastVolts == null || state?.lastVolts == volts || device.currentState("battery")?.value == null || Math.abs(curValVolts - volts) > 0.1) {
|
||||||
def pct = (volts - minVolts) / (maxVolts - minVolts)
|
def pct = (volts - minVolts) / (maxVolts - minVolts)
|
||||||
def roundedPct = Math.round(pct * 100)
|
def roundedPct = Math.round(pct * 100)
|
||||||
if (roundedPct <= 0)
|
if (roundedPct <= 0)
|
||||||
roundedPct = 1
|
roundedPct = 1
|
||||||
result.value = Math.min(100, roundedPct)
|
result.value = Math.min(100, roundedPct)
|
||||||
|
} else {
|
||||||
|
// Don't update as we want to smooth the battery values
|
||||||
|
result = null
|
||||||
|
}
|
||||||
|
state.lastVolts = volts
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user