Why is my piston rounding the answer of a mathematical function to a whole number?


#1

1) Give a description of the problem
I have a piston that is calculating a duration, but when it generates the string to make a POST request, it is either truncating all of the decimal points, or rounding.

2) What is the expected behaviour?
In the example log provided, I would have expected “8.239915” to be the “value” posted to my InfluxDB database.

3) What is happening/not happening?
After calculating 8.239915, it posted “8” to the database.

**4) Post a Green Snapshot of the piston![image|45x37]

5) Attach logs after turning logging level to Full
10/12/2019, 11:59:59 PM +79ms
+0ms ╔Received event [Home].time = 1570950000000 with a delay of -921ms
+149ms ║RunTime Analysis CS > 27ms > PS > 65ms > PE > 57ms > CE
+152ms ║Runtime (45611 bytes) successfully initialized in 65ms (v0.3.10e.20190628) (151ms)
+153ms ║╔Execution stage started
+188ms ║║Comparison (enum) off is (string) on = false (1ms)
+189ms ║║Cancelling condition #27’s schedules…
+190ms ║║Condition #27 evaluated false (11ms)
+191ms ║║Cancelling condition #26’s schedules…
+192ms ║║Condition group #26 evaluated false (state changed) (13ms)
+194ms ║║Cancelling statement #15’s schedules…
+202ms ║║Calculating (string) Total for the day: + (string) 08:14:23 >> (string) Total for the day: 08:14:23
+206ms ║║Total for the day: 08:14:23
+207ms ║║Executed virtual command log (2ms)
+217ms ║║Calculating (long) 29663694 / (long) 3600000 >> (long) 8.239915
+222ms ║║Calculating (string) duration,deviceName=Pool\ Recirc,groupName=Unknown,hubName=Home\ Hub,locationName=Home,unit=hours value= + (string) 8 >> (string) duration,deviceName=Pool\ Recirc,groupName=Unknown,hubName=Home\ Hub,locationName=Home,unit=hours value=8
+225ms ║║Calculating (string) duration,deviceName=Pool\ Recirc,groupName=Unknown,hubName=Home\ Hub,locationName=Home,unit=hours value=8 + (string) >> (string) duration,deviceName=Pool\ Recirc,groupName=Unknown,hubName=Home\ Hub,locationName=Home,unit=hours value=8
+229ms ║║Sending external web request to: xxx.xxx.xxx.xxx:xxxx/write?db=SmartThings
+447ms ║║Executed virtual command httpRequest (219ms)
+453ms ║║Executed virtual command setVariable (3ms)
+457ms ║╚Execution stage complete. (304ms)
+459ms ║Setting up scheduled job for Mon, Oct 14 2019 @ 12:00:00 AM MST (in 86400.463s)
+467ms ╚Event processed successfully (466ms)

REMOVE BELOW AFTER READING
If a solution is found for your question then please mark the post as the solution.


#2

A known oddity in webCore.

Try totalOn/3600000.0


#3

I’ll give that a shot and report back after the next time it runs (midnight)!


#4

I think the long type is a long integer. If you switched to the decimal type, it might cast properly. That said, bthrock’s solution should also work.


#5

Ah, good point and I didn’t catch the use of Long initially. Looking closer, I would probably store the timeOn and timeOff variables as datetime type, eliminate the long variable “now” (using $now directly where needed) and store totalOn as a decimal. But, as you said, the decimal math trick will also work here.

If totalOn was going to grow too large, I’d probably do the math to convert it to minutes or seconds where needed.


#6

Here’s an old thread showing a nice breakdown of the missing decimal when doing math with integers…

As long as one of the numbers in the formula is a decimal, then the answer will be as well…


#7

Thanks for the link! Good info in that thread. I must not have been searching the right keywords to get there myself. Adding a decimal to my second term corrected the issue, as expected.