Battery_level_check script - nil value :/

Moderator: leecollings

Post Reply
karde
Posts: 24
Joined: Monday 04 January 2016 16:57
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.7412
Location: Sweden
Contact:

Battery_level_check script - nil value :/

Post 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
variable.png (60.45 KiB) Viewed 3668 times
These are the name that I have under devices:
devices.png
devices.png (89.19 KiB) Viewed 3668 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)
User avatar
jvdz
Posts: 2328
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Battery_level_check script - nil value :/

Post by jvdz »

You have a mixup on Upeer/Lowercase case characters for VattenSensorKokTemp.
The devicename in the device list is VattensensorKokTemp. :)

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
karde
Posts: 24
Joined: Monday 04 January 2016 16:57
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.7412
Location: Sweden
Contact:

Re: Battery_level_check script - nil value :/

Post by karde »

Goddammit that embarrassing :)

But, I changed it to the below
device.png
device.png (113.04 KiB) Viewed 3623 times
intege.png
intege.png (85.98 KiB) Viewed 3623 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?
User avatar
jvdz
Posts: 2328
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Battery_level_check script - nil value :/

Post 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
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
karde
Posts: 24
Joined: Monday 04 January 2016 16:57
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.7412
Location: Sweden
Contact:

Re: Battery_level_check script - nil value :/

Post 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)
User avatar
jvdz
Posts: 2328
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Battery_level_check script - nil value :/

Post by jvdz »

Is JsonAllDevData.tmp located in directory /var/tmp as I also see the LUA script there?

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
karde
Posts: 24
Joined: Monday 04 January 2016 16:57
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.7412
Location: Sweden
Contact:

Re: Battery_level_check script - nil value :/

Post 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
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: Battery_level_check script - nil value :/

Post 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.
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
karde
Posts: 24
Joined: Monday 04 January 2016 16:57
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.7412
Location: Sweden
Contact:

Re: Battery_level_check script - nil value :/

Post 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!
User avatar
Westcott
Posts: 423
Joined: Tuesday 09 December 2014 17:04
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: UK - Glos
Contact:

Re: Battery_level_check script - nil value :/

Post 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?
Zwave - Sigma Z+ stick, Fibaro, Horstmann, Neo Coolcam, EUROtronic
RFlink - IR detectors and temperatures
Wifi - YeeLights, ESP32s, Anoop sockets
Zigbee - lots with zigbee2mqtt and ZbBridge
User avatar
jvdz
Posts: 2328
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Battery_level_check script - nil value :/

Post 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
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
karde
Posts: 24
Joined: Monday 04 January 2016 16:57
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.7412
Location: Sweden
Contact:

Re: Battery_level_check script - nil value :/

Post 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.
User avatar
jvdz
Posts: 2328
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Battery_level_check script - nil value :/

Post 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
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
karde
Posts: 24
Joined: Monday 04 January 2016 16:57
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.7412
Location: Sweden
Contact:

Re: Battery_level_check script - nil value :/

Post 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 :)!
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: Battery_level_check script - nil value :/

Post 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:

Code: Select all

DomoticzIP="username:[email protected]"
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
mischa
Posts: 74
Joined: Tuesday 07 April 2015 20:32
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8872
Location: Heerhugowaard, The Netherlands
Contact:

Re: Battery_level_check script - nil value :/

Post 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.
Pi 2 - Pi 1 - Razzbery 2.0 - Fibaro FGD-211 - Fibaro FGD-212 - Fibaro FGRGBWM-441 (RGBW Module) - Fibaro FGBS321 (Universal Switch) - FGWPF-102 - TBK Home TZ67-G - Synology DS1515+ - Esp Easy (with wemos D1 mini)
mischa
Posts: 74
Joined: Tuesday 07 April 2015 20:32
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8872
Location: Heerhugowaard, The Netherlands
Contact:

Re: Battery_level_check script - nil value :/

Post by mischa »

Try to install Jq-1.4 but get an error, can anybody help me out.
Knipsel3.PNG
Knipsel3.PNG (5.63 KiB) Viewed 3133 times
Thanks,

Mischa
Pi 2 - Pi 1 - Razzbery 2.0 - Fibaro FGD-211 - Fibaro FGD-212 - Fibaro FGRGBWM-441 (RGBW Module) - Fibaro FGBS321 (Universal Switch) - FGWPF-102 - TBK Home TZ67-G - Synology DS1515+ - Esp Easy (with wemos D1 mini)
mischa
Posts: 74
Joined: Tuesday 07 April 2015 20:32
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8872
Location: Heerhugowaard, The Netherlands
Contact:

Re: Battery_level_check script - nil value :/

Post 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.
Pi 2 - Pi 1 - Razzbery 2.0 - Fibaro FGD-211 - Fibaro FGD-212 - Fibaro FGRGBWM-441 (RGBW Module) - Fibaro FGBS321 (Universal Switch) - FGWPF-102 - TBK Home TZ67-G - Synology DS1515+ - Esp Easy (with wemos D1 mini)
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: Battery_level_check script - nil value :/

Post 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.
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest