We have moved at community.getvera.com

Author Topic: Wind Chill calculator - LUA Math help needed  (Read 3727 times)

Offline resq93

  • Sr. Member
  • ****
  • Posts: 272
  • Karma: +0/-2
Wind Chill calculator - LUA Math help needed
« on: November 24, 2013, 09:09:02 pm »
Trying to calculate windchill into a temp sensor. The math per NOAA is as follows, can someone help me write this equation so LUA can can calculate it?

Ty

http://www.onlineconversion.com/windchill.htm

New Wind Chill Index =
35.74 + 0.6215T - 35.75(V0.16) + 0.4275T(V0.16)

where:
V = wind speed (mph)
T = temperature (F)



Will then be used as follows:

local t = luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1","CurrentTemperature",103)
local v = luup.variable_get("urn:upnp-micasaverde-com:serviceId:Weather1","WindSpeed",98)

local total = MATH GOES HERE

luup.variable_set("urn:upnp-org:serviceId:TemperatureSensor1","CurrentTemperature",total,219)
 
 
« Last Edit: November 24, 2013, 10:12:03 pm by resq93 »

Offline a-lurker

  • Hero Member
  • *****
  • Posts: 872
  • Karma: +66/-8
Re: Wind Chill calculator - LUA Math help needed
« Reply #1 on: November 24, 2013, 10:15:38 pm »
Only a metric version - try this in the Luup test area and see result in log. The result is the same as your formula within about 0.05 of a F degree:

Code: [Select]
local temperatureF   = 0.0 -- in Fahrenheit
local windSpeedMph   = 30.0 -- in mph

local temperature    = (temperatureF - 32.0)/1.8
local windSpeed      = windSpeedMph * 1.60934
local windSpeedPower = math.pow (windSpeed, 0.16)

local windChillIndex = 13.12 + (0.6215*temperature) - (11.37*windSpeedPower) + (0.3965*temperature*windSpeedPower)

luup.log('Wind Chill Index C is '..tostring(windChillIndex), 50)
luup.log('Wind Chill Index F is '..tostring(32+(windChillIndex*1.8)), 50)

return true

Offline resq93

  • Sr. Member
  • ****
  • Posts: 272
  • Karma: +0/-2
Re: Wind Chill calculator - LUA Math help needed
« Reply #2 on: November 24, 2013, 10:44:48 pm »
This seems to work, thank you. Does it look right to you?

Code: [Select]
local temperatureF = luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1","CurrentTemperature",103)


local windSpeedMph = luup.variable_get("urn:upnp-micasaverde-com:serviceId:Weather1","WindSpeed",98)


local temperature    = (temperatureF - 32.0)/1.8
local windSpeed      = windSpeedMph * 1.60934
local windSpeedPower = math.pow (windSpeed, 0.16)


local windChillIndex = 13.12 + (0.6215*temperature) - (11.37*windSpeedPower) + (0.3965*temperature*windSpeedPower)
local total = 32+(windChillIndex*1.8)

luup.variable_set("urn:upnp-org:serviceId:TemperatureSensor1","CurrentTemperature",total,219)

Offline resq93

  • Sr. Member
  • ****
  • Posts: 272
  • Karma: +0/-2
Re: Wind Chill calculator - LUA Math help needed
« Reply #3 on: November 24, 2013, 10:49:20 pm »
By the way, if wind speed is less than 1 MPH it screws up the math completely - Temp now is 19.9 and wind is 0 - Windchill is 48.09!

I know that wind below 3 MPH is considered calm but is there any way to factor for this? So a temp of 19.9 with wind of 0 would yield windchill of 19.9?

Ty

M

Offline a-lurker

  • Hero Member
  • *****
  • Posts: 872
  • Karma: +66/-8
Re: Wind Chill calculator - LUA Math help needed
« Reply #4 on: November 24, 2013, 10:59:43 pm »
Yep - do an if then else, based on wind speed. From wikipedia:

Quote
Windchill temperature is defined only for temperatures at or below 10 ?C (50 ?F) and wind speeds above 4.8 kilometres per hour (3.0 mph)

Offline resq93

  • Sr. Member
  • ****
  • Posts: 272
  • Karma: +0/-2
Re: Wind Chill calculator - LUA Math help needed
« Reply #5 on: November 24, 2013, 11:01:55 pm »
I'm sorry but I'm really not good at all this coding, can you please modify my code below to account for a windspeed of less than 1?

Ty

Code: [Select]
local temperatureF = luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1","CurrentTemperature",103)


local windSpeedMph = luup.variable_get("urn:upnp-micasaverde-com:serviceId:Weather1","WindSpeed",98)


local temperature    = (temperatureF - 32.0)/1.8
local windSpeed      = windSpeedMph * 1.60934
local windSpeedPower = math.pow (windSpeed, 0.16)


local windChillIndex = 13.12 + (0.6215*temperature) - (11.37*windSpeedPower) + (0.3965*temperature*windSpeedPower)
local total = 32+(windChillIndex*1.8)

luup.variable_set("urn:upnp-org:serviceId:TemperatureSensor1","CurrentTemperature",total,219)

Offline a-lurker

  • Hero Member
  • *****
  • Posts: 872
  • Karma: +66/-8
Re: Wind Chill calculator - LUA Math help needed
« Reply #6 on: November 24, 2013, 11:11:29 pm »

Offline resq93

  • Sr. Member
  • ****
  • Posts: 272
  • Karma: +0/-2
Re: Wind Chill calculator - LUA Math help needed
« Reply #7 on: November 25, 2013, 02:16:09 pm »
Thanks for all your help. This is my final working version which dumps the results into a virtual temp sensor.

Code: [Select]
--Windchill Calc

local temperatureF = luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1","CurrentTemperature",103)


local windSpeedMph = luup.variable_get("urn:upnp-micasaverde-com:serviceId:Weather1","WindSpeed",98)


local temperature    = (temperatureF - 32.0)/1.8
local windSpeed      = windSpeedMph * 1.60934
local windSpeedPower = math.pow (windSpeed, 0.16)


local windChillIndex = 13.12 + (0.6215*temperature) - (11.37*windSpeedPower) + (0.3965*temperature*windSpeedPower)
local total = 32+(windChillIndex*1.8)

if (windSpeedMph <= "3") then

luup.variable_set("urn:upnp-org:serviceId:TemperatureSensor1","CurrentTemperature",temperatureF,219)

else

luup.variable_set("urn:upnp-org:serviceId:TemperatureSensor1","CurrentTemperature",total,219)

end

Offline a-lurker

  • Hero Member
  • *****
  • Posts: 872
  • Karma: +66/-8
Re: Wind Chill calculator - LUA Math help needed
« Reply #8 on: November 25, 2013, 04:53:19 pm »
Hi resq93

Your programming is getting better by the day!

Couple points:
  • it's good to define your device IDs at the start, so they are more readable and easier to change later if needed
  • luup.variable_get returns a string that needs to be converted to a number
  • no need to do the maths if we already know what the result will be. ie when the wind speed is below 3 mph
  • indenting you code also makes it more readable.
  • try and not mix up your units - you might crash your Mars Lander

Hope the code below works - I have no way of testing it.

Code: [Select]
-- Windchill calculator
-- by resq93, Nov 2013

local CURRENT_TEMP_ID = 103
local WIND_SPEED_ID   = 98
local WIND_CHILL_ID   = 219

local temperatureF = tonumber(luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1","CurrentTemperature",CURRENT_TEMP_ID))
local windSpeedMph = tonumber(luup.variable_get("urn:upnp-micasaverde-com:serviceId:Weather1","WindSpeed",WIND_SPEED_ID))

local temperature  = (temperatureF - 32.0)/1.8
local windSpeed    = windSpeedMph * 1.60934

local windChillIndex = temperature

-- in metric
if (windSpeed > 4.8) then
    local windSpeedPower  = math.pow (windSpeed, 0.16)
    windChillIndex  = 13.12 + (0.6215*temperature) - (11.37*windSpeedPower) + (0.3965*temperature*windSpeedPower)
end

local windChillIndexF = 32+(windChillIndex*1.8)
luup.variable_set("urn:upnp-org:serviceId:TemperatureSensor1","CurrentTemperature",windChillIndexF,WIND_CHILL_ID)
« Last Edit: November 25, 2013, 05:04:47 pm by a-lurker »

Offline resq93

  • Sr. Member
  • ****
  • Posts: 272
  • Karma: +0/-2
Re: Wind Chill calculator - LUA Math help needed
« Reply #9 on: November 25, 2013, 05:16:07 pm »
Looks cleaner but it is failing. log is showing this error

01   11/25/13 17:07:37.296   LuaInterface::StartEngine failed run: 0 [string "local CURRENT_TEMP_ID = 103..."]:5: bad argument #2 to 'tonumber' (base out of range) <0x3371b680>
01   11/25/13 17:07:37.297   JobHandler_LuaUPnP::RunLua failed: local CURRENT_TEMP_ID = 103


m

Offline a-lurker

  • Hero Member
  • *****
  • Posts: 872
  • Karma: +66/-8
Re: Wind Chill calculator - LUA Math help needed
« Reply #10 on: November 25, 2013, 05:28:37 pm »
Try this - my mistake - looks like it returns a list of variables:

Code: [Select]
-- Windchill calculator
-- by resq93, Nov 2013

local CURRENT_TEMP_ID = 103
local WIND_SPEED_ID   = 98
local WIND_CHILL_ID   = 219

local temperatureFStr = luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1","CurrentTemperature",CURRENT_TEMP_ID)
local windSpeedMphStr = luup.variable_get("urn:upnp-micasaverde-com:serviceId:Weather1","WindSpeed",WIND_SPEED_ID)

local temperatureF    = tonumber(temperatureFStr)
local windSpeedMph    = tonumber(windSpeedMphStr)

local temperature  = (temperatureF - 32.0)/1.8
local windSpeed    = windSpeedMph * 1.60934

local windChillIndex = temperature

-- in metric
if (windSpeed > 4.8) then
    local windSpeedPower  = math.pow (windSpeed, 0.16)
    windChillIndex  = 13.12 + (0.6215*temperature) - (11.37*windSpeedPower) + (0.3965*temperature*windSpeedPower)
end

local windChillIndexF = 32+(windChillIndex*1.8)
luup.variable_set("urn:upnp-org:serviceId:TemperatureSensor1","CurrentTemperature",windChillIndexF,WIND_CHILL_ID)

Offline resq93

  • Sr. Member
  • ****
  • Posts: 272
  • Karma: +0/-2
Re: Wind Chill calculator - LUA Math help needed
« Reply #11 on: November 26, 2013, 11:17:32 am »
This works! Ty.

Now I need to work on a dew point calculator.

Ty

M