My sensor is working! I can’t thank @ogiewon enough for his patience. Also a big thank you to @Saif76 for sharing his code with us.
I wonder if someone can help me to change the DTH code so that the percentage only shows 1 or 2 decimals? I noticed @Saif76’s Device Tile shows only 1 decimal, but for some or other reason, mine shows a gazillion decimals both in respect of the % and the litres remaining:

I’ll at the DTH below to make it easier to take a quick look at the code - I’m sure it will be very easy to format the percentage and volume to limit the decimals.
I have one quirk in that I can’t get the sensor to show more than about 78%. (I tested by setting the height to 100cm (in the DTH configuration) and simulating the water level with a piece of cardboard). It goes up to about 78%, remains at 78% as I move the cardboard closer to the sensor and when it is about 1cm from the sensor, the reading switches to a crazy number like -256%. Below that level, the sensor is very accurate - if the piece of cardboard is 80cm away (with the height set to 100cm), the water level is reported as 20%. Anyone has any idea what could be causing that?
Lastly, I was wondering how difficult it would be to add a setting to the DTH which will allow us to set an offset (to make provision for the distance between the sensor and the maximum water level). The idea is that the sensor shows 100% when the water is at maximum level, even though there is still a few inches/cm between the sensor and the water.)
The DTH:
/**
* Child Ultrasonic Sensor
*
* Copyright 2017 Daniel Ogorchock
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
* for the specific language governing permissions and limitations under the License.
*
* Change History:
*
* Date Who What
* ---- --- ----
*
*
*
*/
metadata {
definition (name: "Child Ultrasonic Sensor", namespace: "ogiewon", author: "Daniel Ogorchock") {
capability "Sensor"
attribute "lastUpdated", "String"
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: "#767676"],
[value: 50, color: "#ffa81e"],
[value: 20, color: "#d04e00"]
])
}
tileAttribute ("device.liters", key: "SECONDARY_CONTROL") {
attributeState "power", label:'Water capacity: ${currentValue} liters', icon: "http://cdn.device-icons.smartthings.com/Bath/bath6-icn@2x.png"
}
}
valueTile("lastUpdated", "device.lastUpdated", inactiveLabel: false, decoration: "flat", width: 6, height: 2) {
state "default", label:'Last Updated ${currentValue}', backgroundColor:"#ffffff"
}
}
preferences {
input name: "height", type: "number", title: "Height", description: "Enter height of tank in cm", required: true
input name: "diameter", type: "number", title: "Diameter", description: "Enter radius of tank", required: true
}
}
def generateEvent(String name, String value) {
//log.debug("Passed values to routine generateEvent in device named $device: Name - $name - Value - $value")
def sensorValue = value as float
def volume = 3.14159 * (diameter/2) * (diameter/2) * height
def capacityLiters = volume / 1000 * 2
sendEvent(name: "liters", value: capacityLiters)
def capacityValue = 100 - (sensorValue/height * 100 )
if(capacityValue != 100)
{
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() {
}