[Puzzle] Integer that is sometimes a string

First of all, i am not a full blown programmer, so bare with me on this one.

A while back I created a driver for the HomeWizard P1 meter, that keeps an eye on power- and gas usage.
Works like a charm for me, so I shared it in the community.
[Hubitat/hwp1_driver.groovy at master · Fanman76/Hubitat · GitHub]

Now another user wanted to use my driver, and had some issue's.
First of all, he has no gasline to his home, but tackled that with making the polling of the gas usage optional.

But my big problem: the actual usage is normaly filled with an integer value, positive or negative.
But is this user's case, the return value is sometimes "NULL".
And yeah, a value "NULL" is not a interger but a sring, so my code go's bust in his case.

is there a way to evaluate is the return value is not an integer, ind if not, then just keep the old value?

In my driver, it is the "active_power_l1_w" attribute.

There's probably a million ways to do it, but since your http response is all strings anyway, I would just test that value as a string:

if (res) {
  //do all your events here rather than checking res? 
  //else if res doesn't exist wouldn't it send all events anyway, just with empty values?
  if (res.active_power_l1_w != "NULL") {
     sendEvent([name: "active_power_l1_w", value: res.active_power_l1_w.toInteger(), unit: "W"]);
  }
}
3 Likes

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.