Page 1 of 1
Error in my scale script
Posted: Saturday 27 May 2017 9:13
by capman
Domoticz version beta # 3.7468 on synology with python plugin
A have a bathscale that's working 4 sometime. All readings are send to domoticz 4 each person. But sudenly a get a error in my log.
[string "commandArray = {}..."]:43: bad argument #1 to 'sub' (string expected, got nil)
A have searching for it , but don't find anything. Line 43 is year = string.sub(s, 1, 4)
Is there someone to have a look for what is wrong ? Thanks
Code: Select all
commandArray = {}
if (devicechanged['Weegschaal']) then
print('scale data received')
data = otherdevices_svalues['Weegschaal']
weightMiddle=75
if tonumber(data) == 0 then
print('Gewicht gelijk aan 0')
else
if (tonumber(data) <= weightMiddle and tonumber(data) > 60 ) then
print('Dit is Sabine')
strDeviceNumber="120"
strDeviceName="Gewicht Sabine"
else
if tonumber(data) >= weightMiddle then
print('Dit is Kris')
strDeviceNumber="119"
strDeviceName="Gewicht Kris"
else
if (tonumber(data) >= 55 and tonumber(data) <= 60 ) then
print('Dit is Robin')
strDeviceNumber="122"
strDeviceName="Gewicht Robin"
else
print('Dit is Milan')
strDeviceNumber="121"
strDeviceName="Gewicht Milan"
end
end
end
end
t1 = os.time()
s = otherdevices_lastupdate[strDeviceName]
year = string.sub(s, 1, 4)
month = string.sub(s, 6, 7)
day = string.sub(s, 9, 10)
hour = string.sub(s, 12, 13)
minutes = string.sub(s, 15, 16)
seconds = string.sub(s, 18, 19)
t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
difference = (os.difftime (t1, t2))
--only update database if 30 seconds has passed since last entry (my oregon gr101 device sends 11 requests over about 10 seconds)
if difference > 30 then
strData = tostring(data) ..'"'
strProgram= 'curl -s -i -H -f --max-time 2 --connect-timeout 2 -0 "Accept: application/json" "http://xxx.xxx.xxx.xxx:xxxx/json.htm?type=command¶m=udevice&hid=2&idx='
strProgram2 ='&dunit=4&dtype=93&dsubtype=1&nvalue=0&svalue='
strCmd = strProgram .. strDeviceNumber .. strProgram2 .. strData
commandArray['SendNotification']= strData
os.execute(strCmd)
end
end
return commandArray
Re: Error in my scale script
Posted: Saturday 27 May 2017 10:04
by jvdz
Wild guess: Variable doesn't get filled so this statement fills s with nil:
s = otherdevices_lastupdate[strDeviceName]
Just add some logging by putting in some print statements before the line in error.
Jos
Re: Error in my scale script
Posted: Saturday 27 May 2017 17:16
by capman
jvdz wrote:Wild guess: Variable doesn't get filled so this statement fills s with nil:
s = otherdevices_lastupdate[strDeviceName]
Just add some logging by putting in some print statements before the line in error.
Jos
I had a print(s) command before line 43 but nohing in the log. Except the error.
The script is in the lua scripting window. It's settings are Weegschaal | Lua | Device.
It's like he recognize the command 'otherdevices_lastupdate' anymore, a think.

Re: Error in my scale script
Posted: Saturday 27 May 2017 17:41
by jvdz
What bout this version? What does it show in the Log?
Code: Select all
commandArray = {}
if (devicechanged['Weegschaal']) then
print('scale data received')
data = otherdevices_svalues['Weegschaal']
weightMiddle = 75
if tonumber(data) == 0 then
print('Gewicht gelijk aan 0')
else
if (tonumber(data) <= weightMiddle and tonumber(data) > 60) then
print('Dit is Sabine')
strDeviceNumber = "120"
strDeviceName = "Gewicht Sabine"
elseif tonumber(data) >= weightMiddle then
print('Dit is Kris')
strDeviceNumber = "119"
strDeviceName = "Gewicht Kris"
elseif (tonumber(data) >= 55 and tonumber(data) <= 60) then
print('Dit is Robin')
strDeviceNumber = "122"
strDeviceName = "Gewicht Robin"
else
print('Dit is Milan')
strDeviceNumber = "121"
strDeviceName = "Gewicht Milan"
end
print('Gewicht is ', data, ' strDeviceName=', strDeviceName)
t1 = os.time()
s = otherdevices_lastupdate[strDeviceName]
print('Lastupdate is ',s)
year = string.sub(s, 1, 4)
month = string.sub(s, 6, 7)
day = string.sub(s, 9, 10)
hour = string.sub(s, 12, 13)
minutes = string.sub(s, 15, 16)
seconds = string.sub(s, 18, 19)
t2 = os.time{year = year, month = month, day = day, hour = hour, min = minutes, sec = seconds}
difference = (os.difftime(t1, t2))
--only update database if 30 seconds has passed since last entry (my oregon gr101 device sends 11 requests over about 10 seconds)
if difference > 30 then
strData = tostring(data)..'"'
strProgram = 'curl -s -i -H -f --max-time 2 --connect-timeout 2 -0 "Accept: application/json" "http://xxx.xxx.xxx.xxx:xxxx/json.htm?type=command¶m=udevice&hid=2&idx='
strProgram2 = '&dunit=4&dtype=93&dsubtype=1&nvalue=0&svalue='
strCmd = strProgram .. strDeviceNumber .. strProgram2 .. strData
commandArray['SendNotification'] = strData
os.execute(strCmd)
end
end
end
return commandArray
Jos
Re: Error in my scale script
Posted: Saturday 27 May 2017 18:53
by capman
Copy / past the script. Executed.
Logs.
- 2017-05-27 18:49:48.275 (RFXtrx433) Weight (Weegschaal)
2017-05-27 18:49:49.696 LUA: scale data received
2017-05-27 18:49:49.696 LUA: Dit is Kris
2017-05-27 18:49:49.696 LUA: Gewicht is
2017-05-27 18:49:49.697 LUA: 82.4
2017-05-27 18:49:49.697 LUA: strDeviceName=
2017-05-27 18:49:49.697 LUA: Gewicht Kris
2017-05-27 18:49:49.697 LUA: Lastupdate is
2017-05-27 18:49:49.697 Error: EventSystem: in Weegschaal: [string "commandArray = {}..."]:32: bad argument #1 to 'sub' (string expected, got nil)
Re: Error in my scale script
Posted: Saturday 27 May 2017 18:58
by jvdz
You are sure you have a device that is called "Gewicht Kris"? ( verify spaces,Caps en Lowercase!)
Jos
Re: Error in my scale script
Posted: Saturday 27 May 2017 19:04
by capman

- weegschaal.JPG (90.13 KiB) Viewed 1644 times
Re: Error in my scale script
Posted: Sunday 28 May 2017 11:07
by jvdz
How did you create the device "Gewicht Kris" as it has an unknown for Hardware?
Shouldn't that be a Dummy Hardware device?
Jos
Re: Error in my scale script
Posted: Sunday 28 May 2017 13:31
by capman
jvdz wrote:How did you create the device "Gewicht Kris" as it has an unknown for Hardware?
Shouldn't that be a Dummy Hardware device?
Jos
I get this code in my browser >
Code: Select all
http://ipdomoticz:port/json.htm?type=command¶m=udevice&hid=2&did=4000&dunit=4&dtype=80&dsubtype=1&nvalue=0&svalue=80
So a creating a new device. This was before the dummy intgration in domoticz. Hmmm.. maybe a try it with a dummy now.
I follow the tutorial from a old forum
http://www.domoticz.com/forum/viewtopic ... 6078#p6078
But it not exist anymore.
Re: Error in my scale script
Posted: Sunday 28 May 2017 13:43
by capman
That did the trick

. Stupid to do this not from the beginning

. Strange that it's not working sudenly anymore

.
But what dummy sensor can a use? I had dummy sensor distence but it's cm or inches. And I can't change icon. Let's try it all. Or there must be
a weight sensor comming

. Thanks 4 thinking this out.
Log
- 2017-05-28 13:35:34.630 LUA: Gewicht Dummy
2017-05-28 13:35:34.631 LUA: Lastupdate is
2017-05-28 13:35:34.631 LUA: 2017-05-28 13:35:27
2017-05-28 13:35:35.883 LUA: scale data received
2017-05-28 13:35:35.884 LUA: Dit is Kris
2017-05-28 13:35:35.957 LUA: Gewicht is
2017-05-28 13:35:35.957 LUA: 82.9
2017-05-28 13:35:35.957 LUA: strDeviceName=
Script
- Spoiler: show
- commandArray = {}
if (devicechanged['Weegschaal']) then
print('scale data received')
data = otherdevices_svalues['Weegschaal']
weightMiddle = 75
if tonumber(data) == 0 then
print('Gewicht gelijk aan 0')
else
if (tonumber(data) <= weightMiddle and tonumber(data) > 60) then
print('Dit is Sabine')
strDeviceNumber = "120"
strDeviceName = "Gewicht Sabine"
elseif tonumber(data) >= weightMiddle then
print('Dit is Kris')
strDeviceNumber = "375"
strDeviceName = "Gewicht Dummy"
elseif (tonumber(data) >= 55 and tonumber(data) <= 60) then
print('Dit is Robin')
strDeviceNumber = "122"
strDeviceName = "Gewicht Robin"
else
print('Dit is Milan')
strDeviceNumber = "121"
strDeviceName = "Gewicht Milan"
end
print('Gewicht is ', data, ' strDeviceName=', strDeviceName)
t1 = os.time()
s = otherdevices_lastupdate[strDeviceName]
print('Lastupdate is ',s)
year = string.sub(s, 1, 4)
month = string.sub(s, 6, 7)
day = string.sub(s, 9, 10)
hour = string.sub(s, 12, 13)
minutes = string.sub(s, 15, 16)
seconds = string.sub(s, 18, 19)
t2 = os.time{year = year, month = month, day = day, hour = hour, min = minutes, sec = seconds}
difference = (os.difftime(t1, t2))
--only update database if 30 seconds has passed since last entry (my oregon gr101 device sends 11 requests over about 10 seconds)
if difference > 30 then
strData = tostring(data)..'"'
strProgram = 'curl -s -i -H -f --max-time 2 --connect-timeout 2 -0 "Accept: application/json" "http://xxx.xxx.xxx.xxx:xxxx/json.htm?type=command¶m=udevice&hid=2&idx='
strProgram2 = '&dunit=4&dtype=93&dsubtype=1&nvalue=0&svalue='
strCmd = strProgram .. strDeviceNumber .. strProgram2 .. strData
commandArray['SendNotification'] = strData
os.execute(strCmd)
end
end
end
return commandArray
Edit: With custom sensor en aslabel Kg. Now I can choose different icons to. Super Happy (and my wife to

)
THANKS !
Re: Error in my scale script
Posted: Sunday 28 May 2017 14:31
by jvdz
Sweet .. now the weight is visible to the whole family so no hiding anymore.
Jos
Re: Error in my scale script
Posted: Sunday 28 May 2017 14:58
by capman
Yep

, and I have it already integrated in my dashticz dashboard.
