1. The Virtual Device Driver
This driver creates the virtual device that holds the temperature, humidity, and the calculated heat index. It requires no configuration once installed; the App handles feeding it the data
/**
* Virtual Heat Index Sensor
*
* A virtual device to hold Temperature, Humidity, and calculated Heat Index.
*/
metadata {
definition (name: "Virtual Heat Index Sensor", namespace: "custom", author: "User") {
capability "TemperatureMeasurement"
capability "RelativeHumidityMeasurement"
capability "Sensor"
capability "Refresh"
capability "Initialize"
attribute "heatIndex", "number"
}
preferences {
input name: "txtEnable", type: "bool", title: "Enable descriptionText logging", defaultValue: true
}
}
void installed() {
log.info "${device.displayName} installed"
}
void updated() {
log.info "${device.displayName} updated"
}
void initialize() {
log.info "${device.displayName} initialized"
}
void refresh() {
if (txtEnable) log.info "${device.displayName} refresh requested. Contacting parent app..."
// Ping the parent app to force a recalculation for this specific device
if (parent) {
parent.childRefreshRequest(device.deviceNetworkId)
} else {
log.warn "No parent app found to handle refresh request."
}
}
void parse(String description) {
// Virtual devices do not parse physical inbound data
}
// Internal method used by the parent app to set all values simultaneously
void setValues(BigDecimal temp, BigDecimal humid, BigDecimal hi) {
// isStateChange: true forces Hubitat to log the data point even if the value hasn't changed.
// This is critical for keeping periodic graphs plotting smoothly without gaps.
if (txtEnable) log.info "${device.displayName} setting temperature to ${temp}°"
sendEvent(name: "temperature", value: temp, unit: "°F", isStateChange: true)
if (txtEnable) log.info "${device.displayName} setting humidity to ${humid}%"
sendEvent(name: "humidity", value: humid, unit: "%", isStateChange: true)
if (txtEnable) log.info "${device.displayName} setting heatIndex to ${hi}°"
sendEvent(name: "heatIndex", value: hi, unit: "°F", isStateChange: true)
}
2. The Hubitat App
This App subscribes to your existing physical sensors, applies any offsets you configure, runs the NWS Rothfusz calculations, and pushes the results to the child devices
/**
* Heat Index Calculator
*
* Calculates the NWS Rothfusz Heat Index for selected Indoor and Outdoor sensors
* and outputs the results to dedicated Virtual Child Devices. Supports multiple
* sensors per location, averaging, and custom attribute names.
*/
definition(
name: "Heat Index Calculator",
namespace: "custom",
author: "User",
description: "Calculates Heat Index using the Rothfusz regression for indoor and outdoor sensors.",
category: "Utility",
iconUrl: "",
iconX2Url: ""
)
preferences {
page(name: "mainPage", title: "Heat Index Calculator", install: true, uninstall: true) {
section("<b>Indoor Sensor Configuration</b>") {
input "indoorTempSensors", "capability.temperatureMeasurement", title: "Select Indoor Temperature Sensor(s)", required: true, multiple: true
input "indoorTempAttr", "text", title: "Indoor Temperature Attribute Name", defaultValue: "temperature", required: true
input "indoorHumidSensors", "capability.relativeHumidityMeasurement", title: "Select Indoor Humidity Sensor(s)", required: true, multiple: true
input "indoorHumidAttr", "text", title: "Indoor Humidity Attribute Name", defaultValue: "humidity", required: true
input "indoorTempOffset", "decimal", title: "Indoor Temperature Offset (°F) [Applied to Average]", defaultValue: 0.0, required: true
input "indoorHumidOffset", "decimal", title: "Indoor Humidity Offset (%) [Applied to Average]", defaultValue: 0.0, required: true
input "indoorRefresh", "enum", title: "Periodic Update Interval", options: ["0":"Disabled", "5":"5 Minutes", "10":"10 Minutes", "15":"15 Minutes", "30":"30 Minutes", "60":"1 Hour"], defaultValue: "0", required: true
}
section("<b>Outdoor Sensor Configuration</b>") {
input "outdoorTempSensors", "capability.temperatureMeasurement", title: "Select Outdoor Temperature Sensor(s)", required: true, multiple: true
input "outdoorTempAttr", "text", title: "Outdoor Temperature Attribute Name", defaultValue: "temperature", required: true
input "outdoorHumidSensors", "capability.relativeHumidityMeasurement", title: "Select Outdoor Humidity Sensor(s)", required: true, multiple: true
input "outdoorHumidAttr", "text", title: "Outdoor Humidity Attribute Name", defaultValue: "humidity", required: true
input "outdoorTempOffset", "decimal", title: "Outdoor Temperature Offset (°F) [Applied to Average]", defaultValue: 0.0, required: true
input "outdoorHumidOffset", "decimal", title: "Outdoor Humidity Offset (%) [Applied to Average]", defaultValue: 0.0, required: true
input "outdoorRefresh", "enum", title: "Periodic Update Interval", options: ["0":"Disabled", "5":"5 Minutes", "10":"10 Minutes", "15":"15 Minutes", "30":"30 Minutes", "60":"1 Hour"], defaultValue: "0", required: true
}
section("<b>Manual Actions</b>") {
input "btnRefresh", "button", title: "Force Immediate Update"
}
section("<b>Logging</b>") {
input "txtEnable", "bool", title: "Enable Info Logging", defaultValue: true
input "logEnable", "bool", title: "Enable Debug Logging", defaultValue: false
}
}
}
void installed() {
if (logEnable) log.debug "Installed with settings: ${settings}"
initialize()
}
void updated() {
if (logEnable) log.debug "Updated with settings: ${settings}"
unsubscribe()
unschedule()
initialize()
}
void appButtonHandler(String btn) {
if (btn == "btnRefresh") {
if (txtEnable) log.info "Manual update triggered from App UI."
forceInitialUpdate()
}
}
void initialize() {
// Create child devices if they do not exist
String indoorChildId = "HeatIndex-Indoor-${app.id}"
String outdoorChildId = "HeatIndex-Outdoor-${app.id}"
if (!getChildDevice(indoorChildId)) {
addChildDevice("custom", "Virtual Heat Index Sensor", indoorChildId, null, [name: "Indoor Heat Index", label: "Indoor Heat Index", isComponent: true])
if (txtEnable) log.info "Created Virtual Device: Indoor Heat Index"
}
if (!getChildDevice(outdoorChildId)) {
addChildDevice("custom", "Virtual Heat Index Sensor", outdoorChildId, null, [name: "Outdoor Heat Index", label: "Outdoor Heat Index", isComponent: true])
if (txtEnable) log.info "Created Virtual Device: Outdoor Heat Index"
}
String iTempAttr = (indoorTempAttr ?: "temperature").trim()
String iHumidAttr = (indoorHumidAttr ?: "humidity").trim()
String oTempAttr = (outdoorTempAttr ?: "temperature").trim()
String oHumidAttr = (outdoorHumidAttr ?: "humidity").trim()
// Subscribe to physical sensors for event-driven updates using specified attributes
if (indoorTempSensors) subscribe(indoorTempSensors, iTempAttr, indoorTempHandler)
if (indoorHumidSensors) subscribe(indoorHumidSensors, iHumidAttr, indoorHumidHandler)
if (outdoorTempSensors) subscribe(outdoorTempSensors, oTempAttr, outdoorTempHandler)
if (outdoorHumidSensors) subscribe(outdoorHumidSensors, oHumidAttr, outdoorHumidHandler)
// Setup cron schedules for periodic updates based on user preferences
if (indoorRefresh != "0") {
if (indoorRefresh == "60") schedule("0 0 * * * ?", indoorTimerHandler)
else schedule("0 0/${indoorRefresh} * * * ?", indoorTimerHandler)
}
if (outdoorRefresh != "0") {
if (outdoorRefresh == "60") schedule("0 0 * * * ?", outdoorTimerHandler)
else schedule("0 0/${outdoorRefresh} * * * ?", outdoorTimerHandler)
}
// Force an initial update slightly delayed so the database finishes creating child devices first
runIn(2, "forceInitialUpdate")
}
void forceInitialUpdate() {
if (indoorTempSensors && indoorHumidSensors) calculateAndPush("indoor")
if (outdoorTempSensors && outdoorHumidSensors) calculateAndPush("outdoor")
}
void indoorTimerHandler() {
if (logEnable) log.debug "Running periodic scheduled update for Indoor Sensors."
calculateAndPush("indoor")
}
void outdoorTimerHandler() {
if (logEnable) log.debug "Running periodic scheduled update for Outdoor Sensors."
calculateAndPush("outdoor")
}
void childRefreshRequest(String dni) {
if (logEnable) log.debug "Received refresh request from child device: ${dni}"
if (dni.contains("Indoor")) {
calculateAndPush("indoor")
} else if (dni.contains("Outdoor")) {
calculateAndPush("outdoor")
}
}
void indoorTempHandler(evt) {
calculateAndPush("indoor")
}
void indoorHumidHandler(evt) {
calculateAndPush("indoor")
}
void outdoorTempHandler(evt) {
calculateAndPush("outdoor")
}
void outdoorHumidHandler(evt) {
calculateAndPush("outdoor")
}
BigDecimal getAverageValue(sensors, String attribute) {
if (!sensors) return null
BigDecimal sum = 0.0
int count = 0
// Handle both single selections and multiple selections natively
def sensorList = sensors instanceof List ? sensors : [sensors]
for (sensor in sensorList) {
def val = sensor.currentValue(attribute)
if (val != null) {
sum += (BigDecimal) val
count++
}
}
if (count == 0) return null
return (sum / count).setScale(2, BigDecimal.ROUND_HALF_UP)
}
void calculateAndPush(String location) {
BigDecimal tempOffset = 0.0
BigDecimal humidOffset = 0.0
BigDecimal avgTemp = null
BigDecimal avgHumid = null
String childId = ""
String iTempAttr = (indoorTempAttr ?: "temperature").trim()
String iHumidAttr = (indoorHumidAttr ?: "humidity").trim()
String oTempAttr = (outdoorTempAttr ?: "temperature").trim()
String oHumidAttr = (outdoorHumidAttr ?: "humidity").trim()
if (location == "indoor") {
avgTemp = getAverageValue(indoorTempSensors, iTempAttr)
avgHumid = getAverageValue(indoorHumidSensors, iHumidAttr)
tempOffset = indoorTempOffset != null ? (BigDecimal) indoorTempOffset : 0.0
humidOffset = indoorHumidOffset != null ? (BigDecimal) indoorHumidOffset : 0.0
childId = "HeatIndex-Indoor-${app.id}"
} else {
avgTemp = getAverageValue(outdoorTempSensors, oTempAttr)
avgHumid = getAverageValue(outdoorHumidSensors, oHumidAttr)
tempOffset = outdoorTempOffset != null ? (BigDecimal) outdoorTempOffset : 0.0
humidOffset = outdoorHumidOffset != null ? (BigDecimal) outdoorHumidOffset : 0.0
childId = "HeatIndex-Outdoor-${app.id}"
}
if (avgTemp == null || avgHumid == null) {
if (logEnable) log.debug "Skipping ${location} calculation: unable to calculate average temperature or humidity."
return
}
BigDecimal t = avgTemp + tempOffset
BigDecimal rh = avgHumid + humidOffset
// Cap humidity bounds between 0 and 100 just in case offsets push it over
if (rh > 100.0) rh = 100.0
if (rh < 0.0) rh = 0.0
BigDecimal heatIndex = calculateRothfusz(t, rh)
if (logEnable) log.debug "Location: ${location.capitalize()} | Avg Temp: ${avgTemp}, Offset Temp: ${t} | Avg Humid: ${avgHumid}, Offset Humid: ${rh} | Calculated Heat Index: ${heatIndex}"
def child = getChildDevice(childId)
if (child) {
child.setValues(t, rh, heatIndex)
} else {
log.error "Could not find child device: ${childId}"
}
}
BigDecimal calculateRothfusz(BigDecimal t, BigDecimal rh) {
// First, try the simple formula
BigDecimal hi = 0.5 * (t + 61.0 + ((t - 68.0) * 1.2) + (rh * 0.094))
// The Rothfusz regression is only valid if the simple HI is 80 F or higher
if (hi >= 80.0) {
hi = -42.379 + (2.04901523 * t) + (10.14333127 * rh) - (0.22475541 * t * rh) -
(0.00683783 * t * t) - (0.05481717 * rh * rh) + (0.00122874 * t * t * rh) +
(0.00085282 * t * rh * rh) - (0.00000199 * t * t * rh * rh)
// NWS Adjustment for Low Humidity and High Temperature
if (rh < 13.0 && t >= 80.0 && t <= 112.0) {
BigDecimal adjustment = ((13.0 - rh) / 4.0) * Math.sqrt((17.0 - Math.abs(t - 95.0)) / 17.0)
hi = hi - adjustment
}
// NWS Adjustment for High Humidity and Lower Temperature
else if (rh > 85.0 && t >= 80.0 && t <= 87.0) {
BigDecimal adjustment = ((rh - 85.0) / 10.0) * ((87.0 - t) / 5.0)
hi = hi + adjustment
}
}
// Round to one decimal place for cleaner logging and graphing
return hi.setScale(1, BigDecimal.ROUND_HALF_UP)
}
Once both are installed, navigate to the App section, add "Heat Index Calculator", and select your respective sensors. It will immediately generate the two child devices. The values will populate on the child devices the next time your physical sensors report a change in temperature or humidity.