' this script reads data from TEMP05 and stores received temperatures into HS virtual devices
' Created by Mitch Matteau 00-01-15
' Modified 02-04-15 : Frank J. Perricone <hawthorn@starband.net>
' Modified 01-11-25 : added DeviceValue saving of temperature results
' Modified 01-09-16 : added capture of relay status, rain counts, wind speed, wind direction and TEMP05 version #
' Modified 01-08-25 : added conditional update of DeviceLastChange and removed restriction for V4.16 or greater firmware
' Modified 01-08-21 : changed to add use of hs.SetDeviceLastChange to record changes
' Modified 01-07-04 : added support for OWWS (V1 or V2) users
' Modified 01-04-02 : added support for 1 humidity sensor
' Modified 01-05-06 : added SetDeviceValue output of temperatures
' Modified 01-05-27 : added support for multiple humidity sensors
'=============================================================
' HS virtual devices used;
' t1 to t19 for DS18S20 sensors
' t20-29 for humidity sensor
' t30 for 0th sensor
' t31 for version
' t32 to t39 for relay status capture
'=============================================================
sub main(data)
'=============================================================
dim i
dim result
dim virt_device
dim prev_result
'=============================================================
' Check if the received data is from a temperature reading
'=============================================================
if mid(data,1,6)="Sensor" then
'hs.WriteLog "TEMP05",data
sensorid = mid(data,9,2)
if left(sensorid,1) = "0" then
sensorid=mid(sensorid,2,1)
end if
' the following line is only required for V4.16 or lower firmware OR if you are not using a V1 Weather Station
' if sensorid="0" then sensorid = "30" ' needed for the zero'th sensor
'
i = Instr(data,"=")
tmp = trim(mid(data,i+1))
if right(tmp,1)="F" or right(tmp,1)="C" then
tmp = left(tmp,(len(tmp)-1))
end if
if CInt(sensorid)<=19 then
virt_device="t"+sensorid '"t" is the house code for the virtual device used
prev_result=hs.DeviceString(virt_device)
end if
'=============================================================
' eliminate false readings
' false readings are replaced with the previous reading
'=============================================================
if left(tmp,3) = "???" then
devstring = prev_result
elseif left(tmp,3) = "185" then '185 is an indicator that the device is not connected
devstring = prev_result
else
devstring = "<img src='icons/temperatures/" & trim(cstr(fix(tmp))) & ".gif' width=16 height=16 align=absmiddle alt='" & tmp & "&deg;' title='" & tmp & "&deg;'> " & tmp & "&deg;"
'devstring = '<img src="icons/temperatures/' & trim(cstr(cint(tmp))) & '.gif> ' & tmp
end if
'=============================================================
' Now save current readings into HomeSeer
'=============================================================
hs.SetDeviceString virt_device,devstring
if devstring<>prev_result then
hs.SetDeviceLastChange virt_device, Now()
hs.SetDeviceValue virt_device,cint(tmp)
end if
end if
'=============================================================
' Humidity Sensor Capture v4.12 or v4.13 software only
'=============================================================
if left(data,11)="Humidity = " then
prev_result=hs.DeviceString("t20")
result = Trim(mid(data,11))
if result="???" then
result=prev_result
hs.Writelog "Humidity","Sensor error"
end if
hs.SetDeviceString "t20",result
if result<>prev_result then
hs.SetDeviceLastChange "t20", Now()
end if
end if

'=============================================================
' Humidity Sensor Capture v4.14, or higher, software only
'=============================================================
if left(data,10)="Humidity #" then
i=Instr(data,"#")
sensorid=mid(data,i+1,2)
if left(sensorid,1) = "0" then
sensorid=mid(sensorid,2,1)
end if
virt_device="t"+CStr(CInt(sensorid)+19)
prev_result=hs.DeviceString(virt_device)
i = Instr(data,"=")
result = Trim(mid(data,i+1))
if result="???" then
result=prev_result
hs.Writelog "Humidity","Sensor error on #"+sensorid
end if
hs.SetDeviceString virt_device,result
if result<>prev_result then
hs.SetDeviceLastChange virt_device, Now()
end if
end if

'=============================================================
' Version capture
' virtual device t31 used to capture the data
'=============================================================
if mid(data,1,6)="TEMP05" then
version_id = "t31"
hs.SetDeviceString version_id,data
hs.SetDeviceLastChange version_id, Now()
end if
'=============================================================
' Relay Status capture
' virtual devices t32 through t39 used to capture the data
'=============================================================
if mid(data,1,5)="Relay" then
i = Instr(data,"=")
data = mid(data,i+1)
for i=1 to 8
virt_device= "t3"+trim(CStr(i+1))
a = trim(hs.StringItem(data,i,","))
if a<>hs.DeviceString(virt_device) then
hs.SetDeviceString virt_device,a
hs.SetDeviceLastChange virt_device,Now()
end if
next
end if
'=============================================================
' Wind and Rain Information capture
'=============================================================
if left(data,5)="=====" then
dim wind_dirn
dim wind_spd
dim wind_gust
dim high_wind
' virtual device list
wind_dirn = "z7"
wind_spd = "z4"
wind_gust ="z1"
high_wind = "v6"
result = mid(data,6)
a = trim(hs.StringItem(result,1,",")) ' wind direction
if a <>"???" then
hs.SetDeviceString wind_dirn,a
hs.SetDeviceValue wind_dirn,0
end if
a = hs.StringItem(result,2,",") 'wind speed
a = trim(left(a,len(a)-3))
if left(a,2)<>"??" then
hs.SetDeviceString wind_spd,a
hs.SetDeviceValue wind_spd,CInt(a)
else
a=0
end if

b = hs.DeviceString(high_wind)
if isnumeric(b) then
b = CInt(hs.DeviceString(high_wind))
else
b = 0
hs.SetDeviceString high_wind,"0"
hs.SetDeviceLastChange high_wind, Now()
end if

if Cint(a) > CInt(b) then
hs.SetDeviceString high_wind,CStr(a)
hs.SetDeviceLastChange high_wind, Now()
hs.WriteLog "HIGH WIND",a+" mph"
hs.CreateVar("HWNDT")
hs.SaveVar "HWNDT",Time()
end if

a = hs.StringItem(result,3,",") ' wind gust
a = trim(left(a,len(a)-3))
hs.SetDeviceString wind_gust,a
hs.SetDeviceValue wind_gust,CInt(a)

'=============================================================
' Rain Counts
'=============================================================
dim rain_history
dim rain_today
dim rain_absolute
rain_history = "v8"
rain_today = "v7"
rain_absolute = "v9"

b = hs.StringItem(result,4,",") ' rain count
if len(b)>2 then
b = trim(left(b,len(b)-2)) ' remove units
c = hs.DeviceString (rain_history) ' History Info
a = hs.StringItem(c,7,",") ' Yesterday's count

if IsNumeric(b) then
l = CSng(b) ' Value of Absolute count

if Not IsNumeric(a) then
k = l ' on error, assume no count
else
k = CSng(a) ' Value of Yesterday's count
end if

if (l-k) > 10 then
a = "0.00" ' can't do more than 9 inches in 1 cycle
else
a = CStr(Round((l-k),2)) ' Result is today's count
end if
end if
end if

if IsNumeric(a) then
hs.SetDeviceString rain_absolute,b ' absolute rain count
hs.SetDeviceLastChange rain_absolute, Now()
hs.SetDeviceString rain_today,a ' Today's count
hs.SetDeviceValue rain_today,CInt(a)
d = ""
for i = 1 to 7
d = d + hs.StringItem(c,i,",")+","
next
d = d + b
hs.SetDeviceString rain_history,d ' Restore history info
hs.SetDeviceLastChange rain_history, Now()
end if
end if

end sub

Return to TEMP05 Project