I try to math a variable

Hello,

i make a json request and write it in a variable

var Number hours = transform("JSONPATH", "$.result.time['hours']", json_time)

in the variable hours stand now a Number, for example 1.

now i try to math the hours with 3600 in a postupdate command

json_currenttime.postUpdate(hours *3600)

but i always get a error, whats the right way to do the math?

thank you

There are many topics to this issue.

for example: Simple rule math help - #5 by watou or Working with maths - #2 by rlkoshak

You have explicit cast it as number.

Yeah i tried this but i cant use

.state as a Number, because its nö item. Its a var.
I dont know how to use it.

Have you Perhaps a example?

No. Transforms always return strings. “Parse” is the relevant process of reading a string into a number.

There are many hundreds of examples in this forum

1 Like

Send it as string:

json_currenttime.postUpdate((hours *3600).toString)

The point is that if variable hours is a string (it is) then you cannot multiply it by 3600.

1 Like

True, missed that!
So, the correct answer is that he has to parseInt, or parseFloat the transformation.

Thank you all, it works

Would you like to post your working version, for the benefit of later readers?

of course.

var Number hours = Float::parseFloat(transform("JSONPATH", "$.result.time['hours']", json_time))
        var Number minutes = Float::parseFloat(transform("JSONPATH", "$.result.time['minutes']", json_time))
        var Number seconds = Float::parseFloat(transform("JSONPATH", "$.result.time['seconds']", json_time))

        json_currenttime.postUpdate(hours *3600 + minutes *60 + seconds)