Page 1 of 1
Battery_level_check script - nil value :/
Posted: Tuesday 26 January 2016 13:36
by karde
Trying to run the script:
https://www.domoticz.com/wiki/Battery_level_check
Have copied this "installed" it under the lua folder, I have installed jq, and have the below values under user variable in domoticz:

- variable.png (60.45 KiB) Viewed 3669 times
These are the name that I have under devices:

- devices.png (89.19 KiB) Viewed 3669 times
I have the following set for the lua script (johan is the one running domoticz)
-rw-rw-r-- 1 johan johan 2446 jan 26 12:50 script_time_battery.lua
and the following for the tmp file:
-rwxrwxrwx 1 johan johan 0 jan 26 12:55 JsonAllDevData.tmp
Is this correct?
Cause when I the script runs I get the following error i domoticz:
2016-01-26 12:55:00.444 Error: EventSystem: /home/johan/domoticz/scripts/lua/script_time_battery.lua:42: attempt to concatenate local 'DeviceIDToCheckBatteryLevel' (a nil value)
Re: Battery_level_check script - nil value :/
Posted: Tuesday 26 January 2016 15:50
by jvdz
You have a mixup on Upeer/Lowercase case characters for VattenSensorKokTemp.
The devicename in the device list is Vatten
sensorKokTemp.
Jos
Re: Battery_level_check script - nil value :/
Posted: Tuesday 26 January 2016 18:14
by karde
Goddammit that embarrassing
But, I changed it to the below

- device.png (113.04 KiB) Viewed 3624 times

- intege.png (85.98 KiB) Viewed 3624 times
Still the same error though :/
2016-01-26 18:10:00.267 Error: EventSystem: /home/johan/domoticz/scripts/lua/script_time_battery.lua:42: attempt to concatenate local 'DeviceIDToCheckBatteryLevel' (a nil value)
Ain't it the name from under "devices", and the name that has a battery icon on it you should use?
Re: Battery_level_check script - nil value :/
Posted: Tuesday 26 January 2016 18:25
by jvdz
Did you check the contents of: /var/tmp/JsonAllDevData.tmp ?
You could add these lines for debugging purposes:
Code: Select all
--Determine battery level
print("DomoticzIP:",DomoticzIP)
print("DomoticzPort:",DomoticzPort)
print("DeviceIDToCheckBatteryLevel:",DeviceIDToCheckBatteryLevel)
print("DeviceName:",DeviceName)
local handle = io.popen("curl 'http://" .. DomoticzIP .. ":" .. DomoticzPort .. "/json.htm?type=devices&rid=" .. DeviceIDToCheckBatteryLevel .. "' 2>/dev/null | /usr/local/bin/jq -r .result[].BatteryLevel")
Jos
Re: Battery_level_check script - nil value :/
Posted: Tuesday 26 January 2016 19:13
by karde
Thanks for your help jvdz, added the above commands in the script but the temp file is still empty (and got the above error).
Tried to remove the JsonAllDevData.tmp and seems like root want to change it so update the permissions on both files as follow:
-rw-rw-rw- 1 root root 0 jan 26 19:05 JsonAllDevData.tmp
-rw-rw-rw- 1 root root 2633 jan 26 19:06 script_time_battery.lua
I get this in domoticz log:
2016-01-26 19:08:00.378 LUA: DomoticzIP:
2016-01-26 19:08:00.378 LUA: 172.16.30.20
2016-01-26 19:08:00.378 LUA: DomoticzPort:
2016-01-26 19:08:00.378 LUA: 8081
2016-01-26 19:08:00.378 LUA: DeviceIDToCheckBatteryLevel:
2016-01-26 19:08:00.378 LUA: DeviceName:
2016-01-26 19:08:00.378 LUA: Flood
2016-01-26 19:08:00.378 Error: EventSystem: /home/johan/domoticz/scripts/lua/script_time_battery.lua:46: attempt to concatenate local 'DeviceIDToCheckBatteryLevel' (a nil value)
Re: Battery_level_check script - nil value :/
Posted: Tuesday 26 January 2016 19:21
by jvdz
Is JsonAllDevData.tmp located in directory /var/tmp as I also see the LUA script there?
Jos
Re: Battery_level_check script - nil value :/
Posted: Tuesday 26 January 2016 19:50
by karde
No sorry,
the lua script in under:
-rw-rw-rw- 1 root root 2633 jan 26 19:12 script_time_battery.lua
johan@domoticz:~/domoticz/scripts/lua$ pwd
/home/johan/domoticz/scripts/lua
and the JsonAllDevData.tmp is under /var/tmp:
-rw-rw-rw- 1 root root 0 jan 26 19:13 JsonAllDevData.tmp
johan@domoticz:/var/tmp$ pwd
/var/tmp
Re: Battery_level_check script - nil value :/
Posted: Tuesday 26 January 2016 20:22
by simonrg
It would probably be a good idea to post your version of the script in a code block here, as line 46 where you have error according to previous post appears to be blank on the wiki.
Re: Battery_level_check script - nil value :/
Posted: Tuesday 26 January 2016 20:31
by karde
Thanks, sure, but I guess that the the reason that's it is line 46 i because I pasted the above print from jvdz.
Code: Select all
--~/domoticz/scripts/lua/script_time_battery.lua
commandArray = {}
-- ==============================
-- Check battery level of devices
-- ==============================
--Uservariables
BatteryLevelSetPoint=99
EmailTo="[email protected]"
ReportHour=19
ReportMinute=13
DomoticzIP="172.16.30.20"
DomoticzPort="8081"
Message = 'Batterijchecker'
DeviceFileName = "/var/tmp/JsonAllDevData.tmp"
-- Time to run?
time = os.date("*t")
if time.hour == ReportHour and time.min == ReportMinute then
-- Update the list of device names and ids to be checked later
os.execute("curl 'http://" .. DomoticzIP .. ":" .. DomoticzPort .. "/json.htm?type=devices&order=name' 2>/dev/null| /usr/local/bin/jq -r '.result[]|{(.Name): .idx}' >" .. DeviceFileName)
BattToReplace = false
-- Retrieve the battery device names from the user variable - stored as name one|name 2|name 3|name forty one
DevicesWithBatteries = uservariables["DevicesWithBatteries"]
DeviceNames = {}
for DeviceName in string.gmatch(DevicesWithBatteries, "[^|]+") do
DeviceNames[#DeviceNames + 1] = DeviceName
end
-- Loop round each of the devices with batteries
for i,DeviceName in ipairs(DeviceNames) do
--Determine device id
local handle = io.popen("/usr/local/bin/jq -r '.\"" .. DeviceName .. "\" | values' " .. DeviceFileName)
local DeviceIDToCheckBatteryLevel = handle:read("*n")
handle:close()
--Determine battery level
print("DomoticzIP:",DomoticzIP)
print("DomoticzPort:",DomoticzPort)
print("DeviceIDToCheckBatteryLevel:",DeviceIDToCheckBatteryLevel)
print("DeviceName:",DeviceName)
local handle = io.popen("curl 'http://" .. DomoticzIP .. ":" .. DomoticzPort .. "/json.htm?type=devices&rid=" .. DeviceIDToCheckBatteryLevel .. "' 2>/dev/null | /usr/local/bin/jq -r .result[].BatteryLevel")
local BattLevel = string.gsub(handle:read("*a"), "\n", "")
handle:close()
print(DeviceName .. ' batterylevel is ' .. BattLevel .. "%")
-- Check batterylevel against setpoint
if tonumber(BattLevel) < BatteryLevelSetPoint then
BattToReplace = true
print("!!!" .. DeviceName .. " batterylevel below " .. BatteryLevelSetPoint .. "%, current " .. BattLevel .. "%")
Message = Message .. '\n *** ' .. DeviceName .. ' battery level is ' .. BattLevel .. '%'
end
end
-- Only send an email if at least one battery fails the test
if(BattToReplace) then
commandArray['SendEmail']='Domoticz Battery Levels#'.. Message .. '#' .. EmailTo
end
end
--LUA default
return commandArray
I am running on Ubuntu, but that shouldn't be ann issue - right..?
Thanks everyone for the assistance!
Re: Battery_level_check script - nil value :/
Posted: Tuesday 26 January 2016 20:47
by Westcott
From your earlier post, the log shows no value on a new line after -
Code: Select all
2016-01-26 19:08:00.378 LUA: DeviceIDToCheckBatteryLevel:
This suggests that DeviceIDToCheckBatteryLevel has a nil value when it is assigned by -
Code: Select all
local DeviceIDToCheckBatteryLevel = handle:read("*n")
Can you do a -
Code: Select all
print("/usr/local/bin/jq -r '.\"" .. DeviceName .. "\" | values' " .. DeviceFileName)
to see if the file name is formed correctly?
Re: Battery_level_check script - nil value :/
Posted: Tuesday 26 January 2016 20:51
by jvdz
Do I see it correctly that JsonAllDevData.tmp is empty which would indicate that the initial retrieval of all devices is failing?
Are the IP and port correct or do you have security enabled for local lan access?
Just try this url in a browser to test:
Code: Select all
http://172.16.30.20:8081/json.htm?type=devices&order=name
Jos
Re: Battery_level_check script - nil value :/
Posted: Tuesday 26 January 2016 21:00
by karde
Thanks guys.. I tried
http://172.16.30.20:8081/json.htm?type= ... order=name
got back :/
401 Unauthorized
Edited the settings under domoticz to allow for local area network, didn't know that you had to do this / that this option was there..
but now it's working

!!
So I guess now I don't have any login page..? that's fine, but strange that you have to disable it to get the script to work, didn't cross my mind..
Thanks everyone for your help!! It's amazing that you put time to help me in this, really appreciate your support!!!
Thanks jvdz and Westcott.
Re: Battery_level_check script - nil value :/
Posted: Tuesday 26 January 2016 21:11
by jvdz
You also could have used 127.0.0.1 as IP address as the script runs on the same hardware as the server and allow that subnet in without any login.
Guess it is quite common to allow the local LAN subnet in without a login and anything external force the login prompt.
Jos
Re: Battery_level_check script - nil value :/
Posted: Tuesday 26 January 2016 21:42
by karde
True, on a small home network this ain't any issue..
Didn't now that you need to allow it under the domoticz GUI for the scripting though..
Thanks again! I'm really happy that It's working

!
Re: Battery_level_check script - nil value :/
Posted: Thursday 28 January 2016 0:23
by simonrg
karde wrote:True, on a small home network this ain't any issue..
Didn't know that you need to allow it under the domoticz GUI for the scripting though..
If you want to keep local authentication, then you shouldn't need to turn it off, just incorporate your username and password in the DomoticzIP variable:
Re: Battery_level_check script - nil value :/
Posted: Wednesday 27 April 2016 15:48
by mischa
I have the same problem, but think it is because my JsonAllDevData.tmp file has not sufficient rights.
cannot open it or change it.
I use WINSCP for editing and file transfer.
Re: Battery_level_check script - nil value :/
Posted: Saturday 25 June 2016 10:10
by mischa
Try to install Jq-1.4 but get an error, can anybody help me out.

- Knipsel3.PNG (5.63 KiB) Viewed 3134 times
Thanks,
Mischa
Re: Battery_level_check script - nil value :/
Posted: Saturday 25 June 2016 11:57
by mischa
After digging around, I found out that I had no compiler installed.
updating first; sudo apt-get update
next install; sudo apt-get install build-essential
after this I could configure and make the install.
Now the battery script is working. Maybe somebody can update the WIKI page.
Re: Battery_level_check script - nil value :/
Posted: Saturday 25 June 2016 12:23
by simonrg
mischa wrote:..... Maybe somebody can update the WIKI page.
Yes, you can update the Wiki page, just log in with your Forum id and click edit on the page.