Getting rid of old/unused State Variables

While modifying a ST driver I ported, there were a bunch of state variables in the original I don't want. I removed them, no issue there.

But I noticed that some of them are still listed in the section in between commands and preferences. See picture.

Is there any way to get rid of those? I'm stumped (again...).

There are 2 ways that I have seen on the forums:

  • Remove the device/app that has the old states and add it back
  • Call a function that you create in the code to clean up the variables using state.remove("oldvariablename")

I tried the 2nd way last night... But maybe I did it wrong, as it didn't work.

I need to take a break. I've messed up so many things in my code lately.. I might have goofed that up too. lol.

1 Like

I have this in the Aeotec Multisensor6 driver I've been maintaining:

private dbCleanUp() {
unschedule()
state.remove("version")
state.remove("sensorTemp")
}

It's just a 'placeholder' method that isn't used. I used it for EXACTLY the same purpose, getting rid of states from a previous driver. I copy paste the needed lines into updated() then click save preferences then go back and remove the lines from updated()

1 Like

Not to dredge up an old topic....

But I am still having weird issues with this.

If I do this it works fine in updated():

state.remove("lastClockSet")

But if I try to do multiple, none of the get deleted. And there are no errors in the log...

Like this:

state.remove("lastBattery")
state.remove("lastBatteryGet")
state.remove("supportedModes")
state.remove("supportedFanModes")

Shouldn't I be able to do more than one at a time??? On the code above, if I just comment out all but one, that one gets removed when the updated() function runs. I can then comment out that one, uncomment out the next one, and it gets removed - and so on.

I also tried this, but it doesn't ever remove anything either:

state.each {key, val ->
log.debug "state key: $key, value: $val"
state.remove("$key")
}

It outputs the $key and $val variables correctly in the log.debug... I would swear that worked in ST...

It should...

Once upon a time I went looking for a usable driver for the Aeon Multisensor6. I tried everything I found, incurring many attributes. I've still got this left in the source:

private dbCleanUp() {
unschedule()
state.remove("version")
state.remove("Version")
state.remove("sensorTemp")
}

I put that into updated() anytime I need -- worked for me for the 12 Multisensors I have. It's just an orphaned method in my driver today.

I wish the hub fw could clear out old states when you change driver. Should be done at the system level no? Any negative to doing it that way?

2 Likes

The driver's don't pre define the states and attributes. They get 'invented on the fly' including calculated names:

 	(1..8).each { n ->
		attribute "valve$n", "enum", ["on", "off"]
		command "on$n"

Yes but the isn't there a way for the system to figure out what state variables are available for the device?

1 Like

I'm also finding this annoying. Did anyone find a solution?

+1 on if anyone can remove this from apps...

state.clear()

However, I don't think it's working... "fixed in next release" I believe.

Thanks for the tip on state.clear(). It seems to be working now. I'm on hub version 2.1.1.122.

Sorry to revive this thread, but state.remove("something") won't work if the driver capability has that state built in. For example, if you have a device that supports both AC power and battery and you try to do state.remove("battery") it won't work. The only way to remove battery as a state is to remove the device and recreate/re-add it. The device won't have that state until the first event with that attribute and then it seems to be there forever more.

I wish there was a way to clear a state though in this situation because I have a device that shows battery on the dashboard because ONE time I plugged it in. I also have some Ring devices that support the battery capability and some show and others don't show battery because in the driver I have diligently never sent a battery event when I shouldn't (until a bug that I'm fixing now and thus this revival).

@mike.maxwell

Is there a way to remove the state even when it is the capability of one of the driver capabilities? In addition to what was tried above I tried getting the states with getCurrentStates() and removing it. Hubitat's com.hubitat.hub.domain.State class doesn't seem to be implementing the equals method or the list is immutable. I also tried device.currentStates but the same thing applies I think.

I wish there was a setCurrentValue or setCurrentState that would take a null or a clearCurrentValue or clearCurrentState. That way I could better support the device drivers that support multiple devices (some with battery and some without) or devices that are installed permanently with cords that don't have batteries in them.

Are you confusing the state’s with the attributes?

I use state .clear() in a number of my drivers just fine.

State is just a variable in the driver and the attributes are ‘states a device is currently in’ which are available to apps to query. Not sure if I confused things more with that explanation though.

No, I don't think I'm confused. I think there is some overlap. As far as I can tell the attributes are retrieved via getCurrentStates() with a class that is called "com.hubitat.hub.domain.State" even. Iterate over device.getCurrentStates() and check device.currentStates if you don't know what I mean.

A more correct way to say a state that is covered by a device capability is just to say attribute though. So, I will refer to them as built-in attributes to hopefully lessen any confusion going forward.

Ya. I think its just some bad naming. state.clear() though works on the state object which I think is just a map or list (or a special version of one of the two) which is separate from the attributes.

That's how I understand it though. If I'm wrong I'm sure one of the devs will chime in and I'll learn something new.

Ah, yeah, I just checked again because it's been a long time since I iterated "state" and you are right. No overlap here.

Okay, so my question is slightly different but since I already @mentioned Mike to come here I'll leave my question here.

To sum up, I would like a way to clear an attribute (or a state from getCurrentStates). The attributes won't show up until the first event and are then there forever. I would like to remove one without removing the device and re-adding so that device drivers that support multiple device types can code around showing attributes that don't really apply on the dashboard for examples.

1 Like

I'm well out of my pay grade but is it possible a driver could be written to do this. Say similar to "Basic Z-Wave Tool"?

One wouldn't have to exclude and repair.

I just tried swapping the driver for my HS-WD200+ and the state variables remain. Now can they be found with a driver, I can't say.

You can't clear events (attribute values) without deleting the device.