Custom number device attribute returning null instead of zero


1) Give a description of the problem
First off, I’m very new to both webcore and smartthings. I customized the device handler for the multi-sensor to add some additional attributes. All that is working fine and I can access the new attributes in webcore. I have a question though about one part of the functionality. The attributes are defined as follows:

attribute "motionAverage", "number"
attribute "motionCount", "number"

My question is, when I set these attributes to be zero, webcore does not see them as zero but instead as null. It seems strange that I can’t set a “number” attribute to zero and have it be returned as a number in webcore.

It’s not really a big deal since I can use int and float as my code shows, but I am wondering, am I doing something wrong or is this expected behavior?

2) What is the expected behavior?
For the attribute to return 0

3) What is happening/not happening?
It’s returning null instead of 0

4) Post a Green Snapshot of the pistonimage

5) Attach any logs (From ST IDE and by turning logging level to Full)

2/10/2019, 8:43:05 PM +232ms
+135ms	║dth:
+157ms	║dth:
+181ms	║force: 0
+204ms	║force: 0

2/10/2019, 8:42:05 PM +230ms
+360ms	║dth: 0.23792630302838122
+386ms	║dth: 41
+412ms	║force: 0.23792630302838122
+447ms	║force: 41


This is probably not what you want to hear, but I learned a long time ago to never program using zeros. Depending on the programming language, it can be interpreted as a 0, as null, as blank, or even (not set) here in webCoRE.


Never using zero in any programming language seems unreasonable. It’s all about understand how the language will treat falsy comparisons, and usually there is a way to still tell exactly what the value is if you need to know.

In this case, checking is {0} is false, but is {null} and is {""} are true. This also seems to be something specific to device attributes, because I can have an integer or decimal variable and logging {$foo} does show a 0 and the comparison checks only return true for 0.


I don’t know how to solve it but there have been cases where the values that webcore sees are coming in as string and the only way around it was to do as you did, force it integer() or decimal() etc.

You’d probably get best advice in the ST forum on how to properly send back the values in the expected format. It would get the most exposure with the device handler hackers :slight_smile:


I agree 100%. It is usually not a big deal when only using one language…

On the other hand, here in the HomeAutomation world, there is so much conversion going back and forth between data originating in rival companies devices… In this realm, it often requires multiple checks, like you pointed out above.

I avoid all that shenanigans by not using zeros whenever possible.