Author Topic: Values for dataType in JSON files for Luup devices  (Read 8484 times)

Offline Ap15e

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 1998
  • Karma: +12/-0
Values for dataType in JSON files for Luup devices
« on: March 18, 2011, 09:14:55 am »
Is there a dataType for float?

Offline mcvflorin

  • Administrator
  • Hero Member
  • *****
  • Posts: 1755
  • Karma: +11/-3
Re: Values for dataType in JSON files for Luup devices
« Reply #1 on: March 18, 2011, 09:21:13 am »
Yes, there are several:
  • r4: 4 Byte float. Same format as float. MUST be between 3.40282347E+38 to 1.17549435E-38.
  • r8: 8 Byte float. Same format as float. MUST be between -1.79769313486232E308 and -4.94065645841247E-324 for negative values, and between 4.94065645841247E-324 and 1.79769313486232E308 for positive values, i.e., IEEE 64-bit (8-Byte) double.
  • number: Same as r8.
  • fixed.14.4: Same as r8 but no more than 14 digits to the left of the decimal point and no more than 4 to the right.
  • float: Floating point number.

Offline Ap15e

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 1998
  • Karma: +12/-0
Re: Values for dataType in JSON files for Luup devices
« Reply #2 on: March 18, 2011, 10:22:00 am »
Thanks for the quick reply.

An additional question:

Must the dataType within <stateVariable>..</stateVariable> match the dataType in the JSON file?

Offline mcvflorin

  • Administrator
  • Hero Member
  • *****
  • Posts: 1755
  • Karma: +11/-3
Re: Values for dataType in JSON files for Luup devices
« Reply #3 on: March 18, 2011, 10:56:00 am »
Not necessarily, but it would be good programming practice. Use number in both places to be on the safe side.

Offline Ap15e

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 1998
  • Karma: +12/-0
Re: Values for dataType in JSON files for Luup devices
« Reply #4 on: March 20, 2011, 02:04:51 pm »
Either number isn't a float, or there is something wrong with the number parser for Luup variables/events, see http://forum.micasaverde.com/index.php?topic=6009.0.
« Last Edit: March 20, 2011, 02:09:07 pm by Ap15e »

Offline mcvflorin

  • Administrator
  • Hero Member
  • *****
  • Posts: 1755
  • Karma: +11/-3
Re: Values for dataType in JSON files for Luup devices
« Reply #5 on: March 21, 2011, 05:34:11 am »
Have you tried float? It's possible that number is treated as an integer.

Offline Ap15e

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 1998
  • Karma: +12/-0
Re: Values for dataType in JSON files for Luup devices
« Reply #6 on: March 21, 2011, 06:00:19 am »
I'll try float - but number seems to work if the first digit is 1 ...

Maybe the parser doesn't like 0.9 and prefers 9E-1?


Offline mcvflorin

  • Administrator
  • Hero Member
  • *****
  • Posts: 1755
  • Karma: +11/-3
Re: Values for dataType in JSON files for Luup devices
« Reply #7 on: March 21, 2011, 06:27:39 am »
Quote
Scene has event 'Cosmical dose rate goes above' [0.1] 'microSv/h'.

Quote
Setting DoseRateCosmical to 0.9 doesn't trigger the event.

Quote
Setting DoseRateCosmical to 1.1 does trigger the event ...

Quote
but number seems to work if the first digit is 1 ...

Maybe 0.1 and 0.9 are rounded to 0 and 1.1 is rounded to 1, so that's why the event condition evaluates to true if DoseRateCosmical is 1.1.

Offline Ap15e

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 1998
  • Karma: +12/-0
Re: Values for dataType in JSON files for Luup devices
« Reply #8 on: March 21, 2011, 06:32:33 am »
It does work for EarthquakeMagnitude > 1.5:

The event doesn't fire for mag <= 1.5, but does fire for mag > 1.5.

Offline Ap15e

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 1998
  • Karma: +12/-0
Re: Values for dataType in JSON files for Luup devices
« Reply #9 on: March 21, 2011, 08:53:13 am »
Here are the results for float (the published DAD V0.13 BETA uses number):

An event should fire, if

Code: [Select]
E1-E3: DoseRate... > 0.1
E4   : DoseRateTotalWarning = 1

E5   : EarthquakeMagnitude > 1.5
E6   : EarthquakeDistance < 7500

Reset the variables:

Code: [Select]
DAD_ID = 16872

luup.variable_set( "urn:upnp-ap15e-com:serviceId:DAD1", 'DoseRateCosmical'    , '0', DAD_ID )
luup.variable_set( "urn:upnp-ap15e-com:serviceId:DAD1", 'DoseRateTerrestrial' , '0', DAD_ID )
luup.variable_set( "urn:upnp-ap15e-com:serviceId:DAD1", 'DoseRateTotal'       , '0', DAD_ID )
luup.variable_set( "urn:upnp-ap15e-com:serviceId:DAD1", 'DoseRateTotalWarning', '0', DAD_ID )

luup.variable_set( "urn:upnp-ap15e-com:serviceId:DAD1", 'EarthquakeMagnitude' , '0', DAD_ID )
luup.variable_set( "urn:upnp-ap15e-com:serviceId:DAD1", 'EarthquakeDistance'  , '10000', DAD_ID )

Now let's set the variables:

Code: [Select]
DAD_ID = 16872

luup.variable_set( "urn:upnp-ap15e-com:serviceId:DAD1", 'DoseRateCosmical'    , '0.9', DAD_ID )
luup.variable_set( "urn:upnp-ap15e-com:serviceId:DAD1", 'DoseRateTerrestrial' , '0.9', DAD_ID )
luup.variable_set( "urn:upnp-ap15e-com:serviceId:DAD1", 'DoseRateTotal'       , '0.9', DAD_ID )
luup.variable_set( "urn:upnp-ap15e-com:serviceId:DAD1", 'DoseRateTotalWarning', '1'  , DAD_ID )

luup.variable_set( "urn:upnp-ap15e-com:serviceId:DAD1", 'EarthquakeMagnitude' , '1.6', DAD_ID )
luup.variable_set( "urn:upnp-ap15e-com:serviceId:DAD1", 'EarthquakeDistance'  , '100', DAD_ID )

Events E4 and E6 fire, events E1-E3 and E5 are missing.

Now let's test norepeat 0: Run the code from above once more
Result: No event fires, but all events should have fired.

Let's change the variables a little bit:

Code: [Select]
DAD_ID = 16872

luup.variable_set( "urn:upnp-ap15e-com:serviceId:DAD1", 'DoseRateCosmical'    , '1.9', DAD_ID )
luup.variable_set( "urn:upnp-ap15e-com:serviceId:DAD1", 'DoseRateTerrestrial' , '1.9', DAD_ID )
luup.variable_set( "urn:upnp-ap15e-com:serviceId:DAD1", 'DoseRateTotal'       , '1.9', DAD_ID )
luup.variable_set( "urn:upnp-ap15e-com:serviceId:DAD1", 'DoseRateTotalWarning', '0'  , DAD_ID )

luup.variable_set( "urn:upnp-ap15e-com:serviceId:DAD1", 'EarthquakeMagnitude' , '1.9', DAD_ID )
luup.variable_set( "urn:upnp-ap15e-com:serviceId:DAD1", 'EarthquakeDistance'  , '101', DAD_ID )

Events E2, E3, E6 fire

Let's change the variables a little bit:

Code: [Select]
DAD_ID = 16872

luup.variable_set( "urn:upnp-ap15e-com:serviceId:DAD1", 'DoseRateCosmical'    , '2.9', DAD_ID )
luup.variable_set( "urn:upnp-ap15e-com:serviceId:DAD1", 'DoseRateTerrestrial' , '2.9', DAD_ID )
luup.variable_set( "urn:upnp-ap15e-com:serviceId:DAD1", 'DoseRateTotal'       , '2.9', DAD_ID )
luup.variable_set( "urn:upnp-ap15e-com:serviceId:DAD1", 'DoseRateTotalWarning', '0'  , DAD_ID )

luup.variable_set( "urn:upnp-ap15e-com:serviceId:DAD1", 'EarthquakeMagnitude' , '2.9', DAD_ID )
luup.variable_set( "urn:upnp-ap15e-com:serviceId:DAD1", 'EarthquakeDistance'  , '101', DAD_ID )

Events E1-E3, E5, E6 fire ...

One possible explanation could be that the event evaluator doesn't know about floats ...

MCV,
please provide a fix. Thanks in advance!

Offline mcvflorin

  • Administrator
  • Hero Member
  • *****
  • Posts: 1755
  • Karma: +11/-3
Re: Values for dataType in JSON files for Luup devices
« Reply #10 on: March 21, 2011, 09:11:11 am »
I found the piece of code where the comparison is made and I can confirm that all numeric values are converted to int, probably for performance reasons.

Knowing this, you could multiply every float value by 10, so instead of 1.5 you would have 15.

I will talk to Aaron to see if he can implement support for floating point values.

Offline Ap15e

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 1998
  • Karma: +12/-0
Re: Values for dataType in JSON files for Luup devices
« Reply #11 on: March 21, 2011, 11:38:13 am »
Thanks for digging into the source code and for confirming that currently there is no support for floating point values for Luup events.

I'll leave DAD as it is and wait for a fix. I could change microSv to nanoSv, but there is no intuitive workaround for EarthquakeMagnitude (deciMagnitude? :-[).

Bug report:
http://bugs.micasaverde.com/view.php?id=1444

BTW, is norepeat 0 working as designed (event doesn't fire if the new value is the same as the old value)?

IMHO, norepeat 0 should fire an event regardless of the previous value, otherwise two events with the same value (e.g., two earthquakes with the same magnitude) cannot be discerned.

Offline mcvflorin

  • Administrator
  • Hero Member
  • *****
  • Posts: 1755
  • Karma: +11/-3
Re: Values for dataType in JSON files for Luup devices
« Reply #12 on: March 21, 2011, 12:00:19 pm »
Quote
I'll leave DAD as it is and wait for a fix.

The fix won't be available in the next release version.

Quote
BTW, is norepeat 0 working as designed (event doesn't fire if the new value is the same as the old value)?

Yes, it's working as designed, though you have a valid point. While the floating point thing isn't hard/risky to implement, changing the norepeat behavior can break some older plugins and applications, so will require extensive compatibility testing.


Offline Ap15e

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 1998
  • Karma: +12/-0
Re: Values for dataType in JSON files for Luup devices
« Reply #13 on: March 21, 2011, 12:17:00 pm »
Quote
[...] changing the norepeat behavior can break some older plugins and applications, so will require extensive compatibility testing.

Valid point. You could add a new keyword, e.g. refire 1 (repeat 1 would be a bad choice :)).

Edit:
New feature request: http://bugs.micasaverde.com/view.php?id=1445
« Last Edit: March 21, 2011, 04:25:25 pm by Ap15e »