Rule Machine Question

Question: I have a switch with a custom attribute which contains elapsed time.

It looks like this:

Question:

In a rule machine condition, I want to check if that Elapsed time is greater than 10 minutes.

Can someone help me with the condition?

(I don't want to specify the date, and I don't see a type called "time" for specifying a time variable that I can compare to my Elapsed Time.)

Thank you in advance.

See if custom attribute will give what you want. Not sure if that will show or not.

My switch will show the correct elapsed time.

I just want to know if the elapsed time is greater than 10 minutes, in a rule, to do certain actions.

In the rule machine create a condition, from the drop-down list select 'custom attribute', select your switch. It will give you a drop-down list of custom attributes. Select your 'elapsed time' (if it's there), choose comparison '>' and so on...

How do i compare a time attribute?

When setting up a hub variable, it wants a specific time of day, not a specific elapsed time value.

Looks like that's just a string, not a "native" numeric type (and there's no native "elapsed time" type), so I think your only options is to use Rule Machine's built-in string operations to parse out the parts you want. Off the top of my head, I'd say this would take several operations, maybe splitting by "h" and "m," then seeing if either the part before "m" is greater than 10 or the part before "h" is greater than 1.

What might be easier: is this an actual switch, or does the data at least come from one? You could use a "sticky trigger" (the "and stays...") option for the switch event on the device a lot more easily. That gives you a trigger, not a condition, but should be pretty reliable. There also is a "Time since event" condition, but I believe it depends on the event still being around in your history (likely if it was only 10-ish minutes, but less likely if "more than 10 minutes" is days or weeks when you happen to check), so it may not work as reliably. If this doesn't lead you where you want, sharing more about the automation you're trying to actually create might give you better ideas.

Is there any way in Hubitat to specify a variable of type "time"?

Time like time (a specific time of day) and/or date, yes, as a hub variable or local variable in a rule. As a device attribute (what your screenshot shows), not really (such an attribute type technically exists in name but hasn't really caught on and isn't standardized, so apps and drivers aren't likely to all treat it the same, so it practically doesn't).

Do you have access to the driver code? Ideally, that driver would split the elapsed time out in to elapsedHours, elapsedMinutes, and elapsedSeconds attributes, as numbers. Then all you would need is to check elapsedMinutes > 10.

Throw the code at AI and have it make those number attributes for you.

Maybe don't have it add an elapsedSeconds attribute, it looks like that driver is sending an event every second as it is, two events per second might be overkill, especially since you just need minutes. Does it really update that attribute every second?

Thanks. Thats exactly what i did. Gemini did an amazing job of doing the driver code. I just told it that i wanted the elapsed time in minutes and it made the driver for me. Very handy.

I should include the code that Gemini generated, for anyone to copy and/or improve.

The code produces a driver for a switch with a custom attribute: elapsedtimeMinutes, which is the amount of minutes since the switch was turned on.

/**

  • Elapsed On-Time Switch Driver for Hubitat
  • 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://apache.org
    
  • Unless required by applicable law or agreed to in writing, 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://apache.org
    
  • Unless required by applicable law or agreed to in writing, 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://apache.org
    
  • 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.

*/

metadata {
definition (name: "Elapsed On-Time Switch", namespace: "custom", author: "AI Collaborator") {
capability "Switch"
capability "Refresh"

    attribute "elapsedOnTimeMinutes", "number"
}

preferences {
    input name: "logEnable", type: "bool", title: "Enable debug logging", defaultValue: true
}

}

def installed() {
log.info "Elapsed On-Time Switch Installed"
initialize()
}

def updated() {
log.info "Elapsed On-Time Switch Updated"
initialize()
}

def initialize() {
unschedule()
if (device.currentValue("switch") == "on") {
// If the device re-initializes while on, restart tracking from the original time if available
if (state.switchOnTime == null) {
state.switchOnTime = now()
}
runEvery1Minute(updateElapsedTime)
} else {
sendEvent(name: "switch", value: "off")
sendEvent(name: "elapsedOnTimeMinutes", value: 0)
state.switchOnTime = null
}
}

def on() {
if (logEnable) log.debug "Turning Switch ON"
sendEvent(name: "switch", value: "on")

state.switchOnTime = now()
sendEvent(name: "elapsedOnTimeMinutes", value: 0)

// Start scheduling the minute counter
runEvery1Minute(updateElapsedTime)

}

def off() {
if (logEnable) log.debug "Turning Switch OFF"
sendEvent(name: "switch", value: "off")

unschedule(updateElapsedTime)
state.switchOnTime = null
sendEvent(name: "elapsedOnTimeMinutes", value: 0)

}

def refresh() {
if (logEnable) log.debug "Refresh requested"
updateElapsedTime()
}

def updateElapsedTime() {
if (device.currentValue("switch") == "on" && state.switchOnTime != null) {
long runningTimeMs = now() - state.switchOnTime
long runningTimeMins = runningTimeMs / 60000

    if (logEnable) log.debug "Updating elapsed time: ${runningTimeMins} minutes"
    sendEvent(name: "elapsedOnTimeMinutes", value: runningTimeMins)
} else {
    if (device.currentValue("elapsedOnTimeMinutes") != 0) {
        sendEvent(name: "elapsedOnTimeMinutes", value: 0)
    }
}

}