WebThingsIO / zigbee-adapter

Zigbee adapter add-on for WebThings Gateway
Mozilla Public License 2.0
46 stars 29 forks source link

Having trouble with API creating floats #226

Open flatsiedatsie opened 4 years ago

flatsiedatsie commented 4 years ago

I'm having some trouble with using the Python API.

I've noticed that if I try to set the brightness of a IKEA light to the value that it already has, it will give a server error.

My code already checks against the existing value, and won't update if the values are the same. But my code thinks 10 and 9.84 are not the same, and tries to update the value. And then it fails, because the gateway does think these values are the same.

9.84251968503937 <- ORIGINAL VALUE
=?=
21 <- INTENDED NEW VALUE
json_dict to PUT to API = {'level': 21}
/things/zb-ec1bbdfffXXXXXXX/properties/level
api result: {'level': 20.866141732283463, 'succes': True} <- SETTING TO 21 SUCCEEDS.. ALMOST?
20.866141732283463
=?=
21
json_dict to PUT to API = {'level': 21}
/things/zb-ec1bbdfffXXXXXXX/properties/level
Error doing http request/loading returned json: HTTPConnectionPool(host='127.0.0.1', port=8080): Read timed out. (read timeout=5)
api result: {'error': 500}
20.866141732283463

I am a bit confused as to what could be going on.

I was trying to find a pattern: 85 -> 85.03937007874016 85 -> 85.03937007874016 85 -> 85.03937007874016

Firstly, it seems it's all perfectly repeatable.

Then I tried to find a pattern. But I couldn't really. Sometimes it's a little up, sometimes a little down.

0 -> 0.3937007874015748 9.84 -> 9.84251968503937 10 -> 9.84251968503937 11 -> 11.023622047244094 20 -> 20.078740157480315 50 -> 50 75 -> 75.19685039370079 85 -> 85.03937007874016 100 -> 100

flatsiedatsie commented 4 years ago

With other devices from other addons I am able to set values accurately.

mrstegeman commented 4 years ago

Is that property defined as an integer instead of a number? Does it have a multipleOf/minimum/maximum that is being violated?

flatsiedatsie commented 4 years ago

I don't know. It's an IKEA lightbulb that only has a brightness and on/off feature. It's used with the Zigbee addon and a Conbee 2 USB stick.

It seems to be a 'level'.

Looking at the HTML in the gateway I see this:

<input type="range" id="slider-130" class="webthing-slider-property-slider" min="0" max="100" step="any" part="slider">

Which seems to indicate a percentage slider that allows any value.

I suspect this is the code in the Zigbee adapter.

And this is the capability code in that same file. It doesn't mention a step. Perhaps adding it wouldn't be a bad idea?

  addBrightnessProperty(node, genLevelCtrlEndpoint) {
    const endpoint = node.activeEndpoints[genLevelCtrlEndpoint];
    this.addProperty(
      node,                           // device
      'level',                        // name
      {// property description
        '@type': 'BrightnessProperty',
        label: 'Brightness',
        type: 'number',
        unit: 'percent',
        minimum: 0,
        maximum: 100,
      },
      endpoint.profileId,             // profileId
      genLevelCtrlEndpoint,           // endpoint
      CLUSTER_ID.GENLEVELCTRL,        // clusterId
      'currentLevel',                 // attr
      'setLevelValue',                // setAttrFromValue
      'parseLevelAttr',               // parseValueFromAttr
      CONFIG_REPORT_INTEGER
    );
  }

Still, that wouldn't explain why the values are off. Perhaps there is some kind of feedback with the HTML pixel values of the slider?

flatsiedatsie commented 4 years ago

Currently my code checks if a value has decimals or not, and then turns the variable into an int or float depending on the outcome of that test.

But that shouldn't matter, since it's turned into JSON for the API anyway? Would sending {'propertyname':24} cause problems if the property is a number type? Should {'propertyname':24.0} be sent to a number?

mrstegeman commented 4 years ago

Does this only happen with Zigbee devices? I've seen this happen before with various other adapters and it ended up being an adapter issue.

flatsiedatsie commented 4 years ago

With other devices from other addons I am able to set values accurately.

Yes it only happens with the Zigbee adapter.