Author Topic: Remove the decimale from temperature reading  (Read 722 times)

Offline michelhamelin

  • Full Member
  • ***
  • Posts: 113
  • Karma: +2/-0
Remove the decimale from temperature reading
« on: March 16, 2017, 03:33:02 pm »
I m trying to remove the decimale from temp reading in Celcius .
Texttospeech in French with Sonos is saying for example  "Ten two Celcius" for 10.2 Celcius it cannot say the POINT.
Here is my 2 script but dont know how to merge them to remove the decimale:

Part 1

    function round(number)
      if (number - (number % 0.1)) - (number - (number % 1)) < 0.5 then
        number = number - (number % 1)
      else
        number = (number - (number % 1)) + 1
      end
     return number
    end



Part 2


local SONOS_ID = 144
local SONOS_SID = "urn:micasaverde-com:serviceId:Sonos1"
local SONOS_ZONES="Galerie"
local Temp0 = luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1","CurrentTemperature", 137)
local TempMin = luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1","CurrentTemperature", 138)
local TempMax1 = luup.variable_get("urn:upnp-micasaverde-com:serviceId:Weather1","Forecast.1.HighTemperature", 136)

if (Temp0 < "0")

then
  Temp0text= " - "
else
   Temp0text = " + "
end

if (TempMin < "0")

then
  TempMintext= " - "
else
   TempMintext = " + "
end

if (TempMax1 < "0")

then
  TempMax1text= " - "
else
   TempMax1text = "  +  "
end

local plug = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1","Status", 15)

if (plug == "0")

then
  volume= 60
else
   volume = 80
end

luup.call_action(SONOS_SID, "Say",
                 {Text= " Actuelleman, il fait,  " .. Temp0text .. Temp0 .. "degr?s ,  minimum se soir" .. TempMintext .. TempMin .." , Demain " .. TempMax1text .. TempMax1 , Language="fr-CA", GroupZones=SONOS_ZONES,
                  Volume=volume, SameVolumeForAll="true"},
                 SONOS_ID)

Thanks for your help

Offline jswim788

  • Sr. Member
  • ****
  • Posts: 483
  • Karma: +23/-2
Re: Remove the decimale from temperature reading
« Reply #1 on: March 16, 2017, 04:22:52 pm »
You probably want to change this from:
Code: [Select]
if (Temp0 < "0")
to this:
Code: [Select]
if (tonumber(Temp0) < 0)

As to your main question, try wrapping your variables in the function you defined:
Code: [Select]
{Text= " Actuelleman, il fait,  " .. Temp0text .. round(tonumber(Temp0)) .. "degr?s ,  minimum se soir" .. TempMI put the 'tonumber' there as well since Temp0 is a string and you want to use it as a number (although lua may do this for you).  Does that do what you want?

If you have AltUI, you can use the test code window to test this or other code.

Offline michelhamelin

  • Full Member
  • ***
  • Posts: 113
  • Karma: +2/-0
Re: Remove the decimale from temperature reading
« Reply #2 on: March 16, 2017, 10:11:50 pm »
It do not work :(

Result: [string "local SONOS_ID = 144..."]:8: attempt to compare number with string


local SONOS_ID = 144
local SONOS_SID = "urn:micasaverde-com:serviceId:Sonos1"
local SONOS_ZONES="Galerie"
local Temp0 = luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1","CurrentTemperature", 137)
local TempMin = luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1","CurrentTemperature", 138)
local TempMax1 = luup.variable_get("urn:upnp-micasaverde-com:serviceId:Weather1","Forecast.1.HighTemperature", 136)

if (tonumber(Temp0) < 0)

then
  Temp0text= " - "
else
   Temp0text = " + "
end

if (TempMin < "0")

then
  TempMintext= " - "
else
   TempMintext = " + "
end

if (TempMax1 < "0")

then
  TempMax1text= " - "
else
   TempMax1text = "  +  "
end

local plug = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1","Status", 15)

if (plug == "0")

then
  volume= 60
else
   volume = 60
end

luup.call_action(SONOS_SID, "Say",
                 {Text= " Actuelleman, il fait,  " .. Temp0text .. round(tonumber(Temp0)) .. "degr?s ,  minimum se soir" .. TempMintext .. TempMin .." , Demain " .. TempMax1text .. TempMax1 , Language="fr-CA", GroupZones=SONOS_ZONES,
                  Volume=volume, SameVolumeForAll="true"},
                 SONOS_ID)



Offline rstrouse

  • Hero Member
  • *****
  • Posts: 781
  • Karma: +27/-9
Re: Remove the decimale from temperature reading
« Reply #3 on: March 16, 2017, 10:56:51 pm »
Maybe try this.

Code: [Select]
function roundNumber(val, decPlaces)
  if(decPlaces ~= nil and decPlaces > 0) then
    return math.floor(((val * 10^decPlaces) + 0.5) / (10^decPlaces))
  else
    return math.floor(val + 0.5)
  end
end
function convertTextInteger(val)
  local n = tonumber(val)
  if(n == nil) return ""
  n = roundNumber(n, 0)
  if(n < 0) then
    return " - " .. math.abs(n)
  return " + " .. n
end

local SONOS_ID = 144
local SONOS_SID = "urn:micasaverde-com:serviceId:Sonos1"
local SONOS_ZONES="Galerie"
local Temp0 = luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1","CurrentTemperature", 137)
local TempMin = luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1","CurrentTemperature", 138)
local TempMax1 = luup.variable_get("urn:upnp-micasaverde-com:serviceId:Weather1","Forecast.1.HighTemperature", 136)

local plug = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1","Status", 15)
local volume = 60
if ((plug or "0") == "0") then
  volume= 60
else
   volume = 60
end
luup.call_action(SONOS_SID, "Say",
   {Text= " Actuelleman, il fait,  " .. convertTextInteger(Temp0) .. "degr?s ,  minimum se soir" .. convertTextInteger(TempMin) .." , Demain "
               .. converTextInteger(TempMax1), Language="fr-CA", GroupZones=SONOS_ZONES,
     Volume=volume, SameVolumeForAll="true"},  SONOS_ID)

I don't have anything to try it with but at least this is not comparing a number with a string.  Also, I don't know the purpose of your volume but it was declared globally.
1xVera3 1.7.619, 4xLinear WT00Z, 3xLinear WS15Z, 1xLeviton VRCZ4, 10xCooper RF9540, 1xLeviton VRFI10, 1xLeviton VP00R, 2xLinear GD00Z-4, 1xGE/Jasco 45612, 1xGE/Jasco 45610,  4xGE/Jasco 45605, 2xYale YRD220-ZW-619, 1xCaddx NX584, Autelis Intellitouch Pool Control, OpenSprinkler

Offline michelhamelin

  • Full Member
  • ***
  • Posts: 113
  • Karma: +2/-0
Re: Remove the decimale from temperature reading
« Reply #4 on: March 16, 2017, 11:12:28 pm »
It give me an error line 10  :(

[string "function roundNumber(val, decPlaces)..."]:10: 'then' expected near 'return'


Maybe try this.

Code: [Select]
function roundNumber(val, decPlaces)
  if(decPlaces ~= nil and decPlaces > 0) then
    return math.floor(((val * 10^decPlaces) + 0.5) / (10^decPlaces))
  else
    return math.floor(val + 0.5)
  end
end
function convertTextInteger(val)
  local n = tonumber(val)
  if(n == nil) return ""
  n = roundNumber(n, 0)
  if(n < 0) then
    return " - " .. math.abs(n)
  return " + " .. n
end

local SONOS_ID = 144
local SONOS_SID = "urn:micasaverde-com:serviceId:Sonos1"
local SONOS_ZONES="Galerie"
local Temp0 = luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1","CurrentTemperature", 137)
local TempMin = luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1","CurrentTemperature", 138)
local TempMax1 = luup.variable_get("urn:upnp-micasaverde-com:serviceId:Weather1","Forecast.1.HighTemperature", 136)

local plug = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1","Status", 15)
local volume = 60
if ((plug or "0") == "0") then
  volume= 60
else
   volume = 60
end
luup.call_action(SONOS_SID, "Say",
   {Text= " Actuelleman, il fait,  " .. convertTextInteger(Temp0) .. "degr?s ,  minimum se soir" .. convertTextInteger(TempMin) .." , Demain "
               .. converTextInteger(TempMax1), Language="fr-CA", GroupZones=SONOS_ZONES,
     Volume=volume, SameVolumeForAll="true"},  SONOS_ID)

I don't have anything to try it with but at least this is not comparing a number with a string.  Also, I don't know the purpose of your volume but it was declared globally.

Offline rstrouse

  • Hero Member
  • *****
  • Posts: 781
  • Karma: +27/-9
Re: Remove the decimale from temperature reading
« Reply #5 on: March 16, 2017, 11:17:30 pm »
Yep forgot a then.

EDIT: And also an end.

Code: [Select]
function roundNumber(val, decPlaces)
  if(decPlaces ~= nil and decPlaces > 0) then
    return math.floor(((val * 10^decPlaces) + 0.5) / (10^decPlaces))
  else
    return math.floor(val + 0.5)
  end
end
function convertTextInteger(val)
  local n = tonumber(val)
  if(n == nil) then
    return ""
  end
  n = roundNumber(n, 0)
  if(n < 0) then
    return " - " .. math.abs(n)
  end
  return " + " .. n
end

local SONOS_ID = 144
local SONOS_SID = "urn:micasaverde-com:serviceId:Sonos1"
local SONOS_ZONES="Galerie"
local Temp0 = luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1","CurrentTemperature", 137)
local TempMin = luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1","CurrentTemperature", 138)
local TempMax1 = luup.variable_get("urn:upnp-micasaverde-com:serviceId:Weather1","Forecast.1.HighTemperature", 136)

local plug = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1","Status", 15)
local volume = 60
if ((plug or "0") == "0") then
  volume= 60
else
   volume = 60
end
luup.call_action(SONOS_SID, "Say",
   {Text= " Actuelleman, il fait,  " .. convertTextInteger(Temp0) .. "degr?s ,  minimum se soir"
     .. convertTextInteger(TempMin) .. " , Demain " .. converTextInteger(TempMax1),    Language="fr-CA",
    GroupZones=SONOS_ZONES, Volume=volume, SameVolumeForAll="true"}, SONOS_ID)

« Last Edit: March 16, 2017, 11:20:16 pm by rstrouse »
1xVera3 1.7.619, 4xLinear WT00Z, 3xLinear WS15Z, 1xLeviton VRCZ4, 10xCooper RF9540, 1xLeviton VRFI10, 1xLeviton VP00R, 2xLinear GD00Z-4, 1xGE/Jasco 45612, 1xGE/Jasco 45610,  4xGE/Jasco 45605, 2xYale YRD220-ZW-619, 1xCaddx NX584, Autelis Intellitouch Pool Control, OpenSprinkler

Offline michelhamelin

  • Full Member
  • ***
  • Posts: 113
  • Karma: +2/-0
Re: Remove the decimale from temperature reading
« Reply #6 on: March 16, 2017, 11:35:31 pm »
You are coming close to the end, still one error line 36

[string "function roundNumber(val, decPlaces)..."]:36: attempt to call global 'converTextInteger' (a nil value)

Thank you for helping me


Yep forgot a then.

EDIT: And also an end.

Code: [Select]
function roundNumber(val, decPlaces)
  if(decPlaces ~= nil and decPlaces > 0) then
    return math.floor(((val * 10^decPlaces) + 0.5) / (10^decPlaces))
  else
    return math.floor(val + 0.5)
  end
end
function convertTextInteger(val)
  local n = tonumber(val)
  if(n == nil) then
    return ""
  end
  n = roundNumber(n, 0)
  if(n < 0) then
    return " - " .. math.abs(n)
  end
  return " + " .. n
end

local SONOS_ID = 144
local SONOS_SID = "urn:micasaverde-com:serviceId:Sonos1"
local SONOS_ZONES="Galerie"
local Temp0 = luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1","CurrentTemperature", 137)
local TempMin = luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1","CurrentTemperature", 138)
local TempMax1 = luup.variable_get("urn:upnp-micasaverde-com:serviceId:Weather1","Forecast.1.HighTemperature", 136)

local plug = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1","Status", 15)
local volume = 60
if ((plug or "0") == "0") then
  volume= 60
else
   volume = 60
end
luup.call_action(SONOS_SID, "Say",
   {Text= " Actuelleman, il fait,  " .. convertTextInteger(Temp0) .. "degr?s ,  minimum se soir"
     .. convertTextInteger(TempMin) .. " , Demain " .. converTextInteger(TempMax1),    Language="fr-CA",
    GroupZones=SONOS_ZONES, Volume=volume, SameVolumeForAll="true"}, SONOS_ID)


Offline rstrouse

  • Hero Member
  • *****
  • Posts: 781
  • Karma: +27/-9
Re: Remove the decimale from temperature reading
« Reply #7 on: March 17, 2017, 12:02:58 am »
Here you go was a runtime typeo.



Code: [Select]
function roundNumber(val, decPlaces)
  if(decPlaces ~= nil and decPlaces > 0) then
    return math.floor(((val * 10^decPlaces) + 0.5) / (10^decPlaces))
  else
    return math.floor(val + 0.5)
  end
end
function convertTextInteger(val)
  local n = tonumber(val)
  if(n == nil) then
    return ""
  end
  n = roundNumber(n, 0)
  if(n < 0) then
    return " - " .. math.abs(n)
  end
  return " + " .. n
end

local SONOS_ID = 144
local SONOS_SID = "urn:micasaverde-com:serviceId:Sonos1"
local SONOS_ZONES="Galerie"
local Temp0 = luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1","CurrentTemperature", 137)
local TempMin = luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1","CurrentTemperature", 138)
local TempMax1 = luup.variable_get("urn:upnp-micasaverde-com:serviceId:Weather1","Forecast.1.HighTemperature", 136)

local plug = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1","Status", 15)
local volume = 60
if ((plug or "0") == "0") then
  volume= 60
else
   volume = 60
end
luup.call_action(SONOS_SID, "Say",
   {Text= " Actuelleman, il fait,  " .. convertTextInteger(Temp0) .. "degr?s ,  minimum se soir"
     .. convertTextInteger(TempMin) .. " , Demain " .. convertTextInteger(TempMax1),    Language="fr-CA",
    GroupZones=SONOS_ZONES, Volume=volume, SameVolumeForAll="true"}, SONOS_ID)

1xVera3 1.7.619, 4xLinear WT00Z, 3xLinear WS15Z, 1xLeviton VRCZ4, 10xCooper RF9540, 1xLeviton VRFI10, 1xLeviton VP00R, 2xLinear GD00Z-4, 1xGE/Jasco 45612, 1xGE/Jasco 45610,  4xGE/Jasco 45605, 2xYale YRD220-ZW-619, 1xCaddx NX584, Autelis Intellitouch Pool Control, OpenSprinkler

Offline michelhamelin

  • Full Member
  • ***
  • Posts: 113
  • Karma: +2/-0
Re: Remove the decimale from temperature reading
« Reply #8 on: March 17, 2017, 12:27:46 am »
YOU ARE MY HERO 8) :),

I wish i can help you one day

Thank very much

Offline akbooer

  • Master Member
  • *******
  • Posts: 5124
  • Karma: +221/-67
  • "Less is more"
Re: Remove the decimale from temperature reading
« Reply #9 on: March 17, 2017, 03:14:34 am »
Glad you finally got it going.

Just FYI, Lua has built-in number/text conversion routines, so you could replace this:

Code: [Select]
function roundNumber(val, decPlaces)
  if(decPlaces ~= nil and decPlaces > 0) then
    return math.floor(((val * 10^decPlaces) + 0.5) / (10^decPlaces))
  else
    return math.floor(val + 0.5)
  end
end
function convertTextInteger(val)
  local n = tonumber(val)
  if(n == nil) then
    return ""
  end
  n = roundNumber(n, 0)
  if(n < 0) then
    return " - " .. math.abs(n)
  end
  return " + " .. n
end

with this:

Code: [Select]
local function convertTextInteger(val)
    return (("%+0.0f"): format (val): gsub("^.", "%1 "))
end
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P.
Razberry, MySensors Arduino, HomeWave, AltUI, DataYours, openLuup, ZWay, ZeroBrane Studio.