I have a morning announcement that announces the high/low temperatures of the day and tomorrow.
Right now the TTS sound stupid when there are decimal values like 13.2 degrees. I would like this value to be rounded to 13 degrees before I add it to the text string. I’m not so good at coding so I have no idea how to do this using lua.
I would greatly appreciate it if someone could help me with the lua code, here is how I get to values to a variable now:
Can I just declare the local variable as integer somehow? It would be nice ofcourse if a value say 13.8 would be rounded to 14 instead of just leaving out the decimal value.
local OwTempTodayHigh= luup.variable_get(TEMP_SID, “TodayMaxTemp”, 548)
local OwTempTodayLow= luup.variable_get(TEMP_SID, “TodayLowTemp”, 548)
local OwTempTomorrowHigh= luup.variable_get(TEMP_SID, “TomorrowHighTemp”, 548)
local OwTempTomorrowLow= luup.variable_get(TEMP_SID, “TomorrowLowTemp”, 548)
You have at least three easy options
- the % remainder operator,
- the math.floor() function,
- the string.format() function.
For example, using #3 above:
local x = {0, 1.3, 1.6, -1.3, -2.6}
for _,n in pairs (x) do
print (n, string.format ("%0.0f", n))
end
gives this:
0 0
1.3 1
1.6 2
-1.3 -1
-2.6 -3
1 Like
Easy for you to say!
In other words if I replace what I use right now:
luup.call_action(LS_SID, "Say", {Text = string.format("God morgon! Idag blir det %s med en högsta temperatur på %s grader och en lägsta temperatur på %s grader. I morgon blir det %s med en högsta temperatur på %s grader och en lägsta temperatur på %s", currentCondition, OwTempTodayHigh, OwTempTodayLow, tomorrowCondition, OwTempTomorrowHigh, OwTempTomorrowLow) ,Volume=35}, AV_DEV)
With the following:
luup.call_action(LS_SID, "Say", {Text = string.format("God morgon! Idag blir det %s med en högsta temperatur på %0.0f grader och en lägsta temperatur på %0.0f grader. I morgon blir det %s med en högsta temperatur på %0.0f grader och en lägsta temperatur på %0.0f", currentCondition, OwTempTodayHigh, OwTempTodayLow, tomorrowCondition, OwTempTomorrowHigh, OwTempTomorrowLow) ,Volume=35}, AV_DEV)
That would do it?
EDIT: Failed to test code, please try again.
EDIT2:Found the problem some of the variables were already strings so they need to be %s
So that does do it, then?
Good news if so!
Yepp, just confirmed that it is working.