Yep, specs on that sensor are 25 to 450cm (10 - 177in). I might run into a similar issue as when full I think the sensor will be about 6in from the top of the water. Here’s the changes I made to the device handler and the code. Include airgap preference and some tile additions. I do the inputs and calcs in CM then convert to gallons.
metadata {
definition (name: "Child Ultrasonic Sensor, Tank Gallons", namespace: "Me", author: "Me") {
capability "Sensor"
attribute "lastUpdated", "String"
attribute "ultrasonic", "Number"
command "generateEvent", ["string", "string"]
}
tiles(scale: 2) {
multiAttributeTile(name: "ultrasonic", type: "generic", width: 6, height: 4, canChangeIcon: true) {
tileAttribute("device.ultrasonic", key: "PRIMARY_CONTROL") {
attributeState("ultrasonic", label: '${currentValue}%', unit:"%", defaultState: true,
backgroundColors: [
[value: 80, color: "#44b621"], // Dark Green
[value: 60, color: "#f1d801"], // Dark Yellow
[value: 40, color: "#d04e00"], // Orange
[value: 20, color: "#bc2323"] // Red
])
}
tileAttribute ("device.gallons", key: "SECONDARY_CONTROL") {
attributeState "power", label:'Remaining: ${currentValue} gallons', icon: "http://cdn.device-icons.smartthings.com/Bath/bath6-icn@2x.png"
}
}
valueTile("gallons", "device.gallons", inactiveLabel: false, decoration: "flat", width: 2, height: 2) {
state "default", label:'${currentValue} Left', unit:"gal", backgroundColor:"#000000"
}
valueTile("capacity", "device.capacity", inactiveLabel: false, decoration: "flat", width: 2, height: 2) {
state "default", label:'${currentValue} Total', unit:"gal", backgroundColor:"#d04e00"
}
valueTile("lastUpdated", "device.lastUpdated", inactiveLabel: false, decoration: "flat", width: 2, height: 2) {
state "default", label:'Last Updated ${currentValue}', backgroundColor:"#ffffff"
}
}
preferences {
input name: "height", type: "number", title: "Height", description: "Enter height of tank full level in cm", required: true
input name: "diameter", type: "number", title: "Diameter", description: "Enter diameter of tank in cm", required: true
input name: "airgap", type: "number", title: "AirGap", description: "Enter Sensor Height Above Full Level", required: true
}
}
def generateEvent(String name, String value) {
log.debug("Passed values to routine generateEvent in device named $device: Name - $name - Value - $value")
double sensorValue = value as float
//Tank total Capacity
def volume = 3.14159 * (diameter/2) * (diameter/2) * height // Volume of Tank
double capacityLiters = volume / 3785 // divide by 3785 to get total capacity in gallons
capacityLiters = capacityLiters as int // Remove decimals
sendEvent(name: "capacity", value: capacityLiters) // This is the total capacity when full
// Ramaining in Tank
double remainingVolume = (volume - (3.14159 * (diameter/2) * (diameter/2) * (sensorValue-airgap))) / 3785 // divide by 3785 to get gallons
remainingVolume = remainingVolume.round(2)
sendEvent(name: "gallons", value: remainingVolume) // Use this for how much is left in the tank
// Tank Percent full
double capacityValue = 100 - ((sensorValue-airgap)/height * 100 ) // Get the percent full
if(capacityValue != 100)
{
capacityValue = capacityValue.round(2)
sendEvent(name: name, value: capacityValue)
}
// Update lastUpdated date and time
def nowDay = new Date().format("MMM dd", location.timeZone)
def nowTime = new Date().format("h:mm a", location.timeZone)
sendEvent(name: "lastUpdated", value: nowDay + " at " + nowTime, displayed: false)
}
def installed() {
}