Error waterflow script

Moderator: leecollings

Post Reply
remko2000
Posts: 167
Joined: Thursday 28 December 2017 14:38
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Broek op Langedijk
Contact:

Error waterflow script

Post by remko2000 »

I try to implant this code:

Code: Select all

commandArray = {}

-- IDX Waterflow
jIDX = 235

-- Begintijd in uservariables zetten
if  (uservariables['begin'] == 0)
    then
        -- print ("begin instellen")
        commandArray['Variable:begin'] = tostring(1)
        commandArray['Variable:begintijd']= tostring(os.time())
end        

-- Decimalen
function round(num, numDecimalPlaces)
   local mult = 10^(numDecimalPlaces or 0)
   return math.floor(num * mult + 0.5) / mult
end

-- timedifference
function timedifference(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)
     t1 = os.time()
     t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
    difference = os.difftime (t1, t2)
    return difference
end


-- Script
if (devicechanged['GPIO Watermeter'] == 'Off')
   then 
       -- print ("Watermeter gaat naar Off")
       btijd = uservariables['begintijd']
       duur = os.time() - btijd
       lpm = 60 /duur
       -- print (lpm)
       lpm = round(lpm, 2)
       -- print (btijd)
       -- print (os.time() - btijd)
       -- print (os.time())
       -- print (duur)
       -- print (lpm)
       commandArray['Variable:begintijd']= tostring(os.time())
       commandArray['UpdateDevice'] = ''..jIDX..'|0|'..lpm..''
   else
        -- Keep a live device and set flow to zero
        if (timedifference(otherdevices_lastupdate["GPIO Watermeter"]) > 300)
           then
               -- print("tijd langer dan 300")
               flow = 0
               -- commandArray['Variable:begintijd']= tostring(os.time() - 300)
               commandArray['UpdateDevice'] = ''..jIDX..'|0|'..flow..''
        end
end       

return commandArray
But I get this error:
‘ Error: EventSystem: in Waterflow: [string “commandArray = {}…”]:22: bad argument #1 to ‘sub’ (string expected, got nil)’
line 22= 'year = string.sub(s, 1, 4)'

I use domoticz V4.10007 on a raspberry pi 3.
I searched for a solutions but I'm stuck. What can I change in this code?
remko2000
Posts: 167
Joined: Thursday 28 December 2017 14:38
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Broek op Langedijk
Contact:

Re: Error waterflow script

Post by remko2000 »

I tried to verify my code with a codechecker and get back:
line 7: attempt to index global 'uservariables' (a nil value)
stack traceback:
t.lua:7: in main chunk
[C]: ?
I'm a beginner with LUA so I do not know how to solve this? Is there someone who can help me?
juuzo
Posts: 20
Joined: Monday 15 October 2018 13:19
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Error waterflow script

Post by juuzo »

You are trying to parse string in variable s and you dont seem to have any variable s. what time difference you would like to calculate?
remko2000
Posts: 167
Joined: Thursday 28 December 2017 14:38
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Broek op Langedijk
Contact:

Re: Error waterflow script

Post by remko2000 »

I'm trying to follow this ' how to' :
https://ehoco.nl/waterflowmeter-in-domoticz/

I think it is the difference between the moment the water starts to run and the water stops again.
I have followed exactly the how to, so it is strange that it does not work
juuzo
Posts: 20
Joined: Monday 15 October 2018 13:19
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Error waterflow script

Post by juuzo »

Okay now i found it. So here you read that timedifference(s) timedifference(otherdevices_lastupdate["GPIO Watermeter"]
Do you have device called GPIO Watermeter? Note this is case sensitive.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest