marc1310
(marc schlĂĽter)
March 25, 2021, 11:18am
1
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
markymark
(Mark Baumann)
March 25, 2021, 11:38am
2
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.
marc1310
(marc schlĂĽter)
March 25, 2021, 11:54am
3
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?
rossko57
(Rossko57)
March 25, 2021, 12:07pm
4
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
rule "Energy received"
when
Item Energy_received_message changed
then
var Number Pplus_ = 5.0
var Number Pminus_ = 1.0
var Number Pdiff_ = Pplus_ - Pminus_
logInfo('rule', curstate.toString)
val String json = (Energy_received_message.state as StringType).toString
logInfo('rule', json)
var Number Pplus = Float::parseFloat(transform("JSONPATH", "$.Pplus", json))
logInfo('rule Pplus', Pplus.toString)
var Number Pminus = Float::parseFloat(transform("JSONPATH"…
1 Like
Send it as string:
json_currenttime.postUpdate((hours *3600).toString)
rossko57
(Rossko57)
March 25, 2021, 12:10pm
6
The point is that if variable hours
is a string (it is) then you cannot multiply it by 3600.
1 Like
marc1310:
h 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
True, missed that!
So, the correct answer is that he has to parseInt, or parseFloat the transformation.
rossko57
(Rossko57)
March 25, 2021, 6:09pm
9
Would you like to post your working version, for the benefit of later readers?
marc1310
(marc schlĂĽter)
March 25, 2021, 8:48pm
10
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)