Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


  • 0

Question

Posted

hope it can be done! :)

 

a,b,c=2,4,6

input='a' --can collect as 'a' 'b' 'c'

for _,v in ipairs({{a,'a',A},{b,'b',B},{c,'c',C}})do

  if input==v then v[3]=v[3]+1 end

  if v[3]>v[1]then send=true end

end

 

the part v[3]=v[3]+1 i can't get it to work

 

thx

2 answers to this question

Recommended Posts

  • 0
Posted (edited)
On 7/9/2023 at 2:59 AM, Kage said:

hope it can be done! :)

 

a,b,c=2,4,6

A

input='a' --can collect as 'a' 'b' 'c'

for _,v in ipairs({{a,'a',A},{b,'b',B},{c,'c',C}})do

  if input==v then v[3]=v[3]+1 end

  if v[3]>v[1]then send=true end

end

 

the part v[3]=v[3]+1 i can't get it to work

 

thx

You have too many errors...

First of all v is a table, so how you compare string to table (input == v)?

You should add another for loop inside.

Try to define A,B,C also.

Just for test case, have defined A,B,C=1,2,3

copy the code below and run it.

 

Please login or register to see this code.

Now it works with no error. But I don't understand what you're trying to achieve, I mean don't follow the logic.😙

Please play with the numbers you need... at least the code is error free from searching point of view.

Edited by cag014
  • 0
  • Inquirer
  • Posted

    to fast typing at my end:)

     

    what i try to do is ad +1 to a global every time a specific letter is called. zo in this case if 'c' is called then C+1 C=1 if 'c' is called again then C+1 C=2 when C=5 then send etc..... and C=0

     

    my script is for my watermeter hour day month year when specific  stand is past i get notification  but not more then say 5 times in a hour or a day  etc....

     

    Here is the QA if you are interested:

     

     

     

    ------------------------------------------------------------------
    -- TOP SCRIPT --
    ------------------------------------------------------------------
    _f=fibaro
    jT=json.decode((_f.getGlobalVariable('HomeTable')))
    Debug=function(color,Message,text)print(string.format('<font color='..color..'>'..Message..'</font>'))end
    __TAG = 'QA_'.._f.getName(plugin.mainDeviceId)..' ID:'..plugin.mainDeviceId
    telegramQA=jT.startQA.telegram
    icon='🚰'
    high='⬆️'
    low='⬇️'
    equal='↔️'
    tableName='WaterHouseTable'
    idGlobals={tableName}
    ------------------------------------------------------------------
    -- Telegram --
    ------------------------------------------------------------------
    telegram=function()local msg=icon..' QA_'.._f.getName(plugin.mainDeviceId)..': '..Message local chat_ID='141284649'_f.call(telegramQA,'sendMessage',msg, chat_ID)end
    ------------------------------------------------------------------
    -- Info --
    ------------------------------------------------------------------
    cS=257953 -- watermeterstand
    gemPP=84 --gem water liter per person
    tPP=5 -- total pesons
    mW=gemPP*tPP
    maxH,maxD,maxM,maxY=((mW/5)*2),mW,(mW*30),(mW*365)
    sH,sD,sM,sY=false,false,false,false
    debug=false
    --jI=json.decode(_f:getGlobalValue('IpTable'))
    _ip='192.168.1.45'
    _port='82'
    bugError=0
    totalBugErrors=5
    -----------------------------------------------
    --icon= 1009--jT.icon.waterMeter
    --self:updateProperty('deviceIcon',icon)
    ------------------------------------------------------------------
    -- Controle for Globals --
    ------------------------------------------------------------------
    if #idGlobals~=0 then
        for _,in ipairs(idGlobals)do
            if _f.getGlobalVariable(v)==nil or _f.getGlobalVariable(v)=='' then
                Message=v..' rede: [nil] global is niet aanwezig!'telegram()
                for _,in ipairs(idGlobals)do
                    if(_f.getGlobalVariable(v)~=nil and _f.getGlobalVariable(v)~='')then b=_f.getGlobalVariable(v)else b=nil end
                    function checkGlobalVar(var)local global=false
                        if b==nil or b=={}then b=api.get('/globalVariables')or{}else global=true end
                        for k,in ipairs(b)do if v.name==var then global=true break end end return global
                    end 
                    if not checkGlobalVar(v)then
                        api.post('/globalVariables/',{name=v})
                        Message='Global making: '..v..' Has been made!'
                        Debug('orange',Message)
                        telegram()
                        _f.setGlobalVariable(v,'OFF')
                    end
                end
            end
        end
    end
    ------------------------------------------------------------------
    -- SetTable --
    ------------------------------------------------------------------
    setTable=function()
        statusTable={cS=cS,h_Stand=h_Stand,lh_Stand=lh_Stand,d_Stand=d_Stand,ld_Stand=ld_Stand,m_Stand=m_Stand,lm_Stand=lm_Stand,y_Stand=y_Stand,ly_Stand=ly_Stand}
        jstatusTable=json.encode(statusTable)
        _f.setGlobalVariable(tableName,jstatusTable)Debug('yellow','Table is filled!')
    end
    setTableReset=function()
        statusTable={cS=cS,h_Stand=0,lh_Stand=0,d_Stand=0,ld_Stand=0,m_Stand=0,lm_Stand=0,y_Stand=0,ly_Stand=0}
        jstatusTable=json.encode(statusTable)
        _f.setGlobalVariable(tableName,jstatusTable)
    end
    if _f.getGlobalVariable(tableName)=='OFF'then setTableReset()Message='LET OP, GEEN'..tableName telegram()end
    ------------------------------------------------------------------
    -- Globals --
    ------------------------------------------------------------------
    globals=function()send=false
        h=os.date('%M')d=os.date('%H:%M')m=os.date('%d %H:%M')y=os.date('%d %m %H:%M')datum=os.date('%d/%m/%Y %H:%M')
        WHT=_f.getGlobalVariable(tableName)
        if #WHT~=0 and WHT~='OFF'then WHT=json.decode((_f.getGlobalVariable(tableName)))
            h_Stand=WHT.h_Stand lh_Stand=WHT.lh_Stand
            d_Stand=WHT.d_Stand ld_Stand=WHT.ld_Stand
            m_Stand=WHT.m_Stand lm_Stand=WHT.lm_Stand
            y_Stand=WHT.y_Stand ly_Stand=WHT.ly_Stand
            if WHT.cS==0 then cS=cS else cS=WHT.cS end
        end
    end
    ------------------------------------------------------------------
    -- SetStand --
    ------------------------------------------------------------------
    function QuickApp:SET(t,P)
        if t=='Hour'then h_Stand=standT if P=='print'then lh_Stand=total Debug('green',total..'L')end
        elseif t=='Day'then d_Stand=standT if P=='print'then ld_Stand=total Debug('green',(ld_Stand/1000)..'M3')end
        elseif t=='Month'then m_Stand=standT if P=='print'then lm_Stand=total Debug('green',(lm_Stand/1000)..'M3')end
        elseif t=='Year'then y_Stand=standT if P=='print'then ly_Stand=total Debug('green',(ly_Stand/1000)..'M3')end
        end
    end
    ------------------------------------------------------------------
    -- Data get --
    ------------------------------------------------------------------
    function QuickApp:getDeviceInfo()globals()
        self.http:request('http://'.._ip..':'.._port ..'/watermeter/api/read',{
        options={method = 'GET'},success=function(response)result=json.decode(response.data)bugError=0 if debug then self:debug(response.data)end
        Wifi=result.wifi_rssi
        factor=result.watermeter_pulse_factor
        value=result.watermeter_value
        count=tonumber(result.watermeter_pulsecount)
        stand=tonumber(string.format('%.0f',count-cS)) if debug then print('cS = '..count..' - '..cS..' = '..stand)end
        cS=cS+stand
        c=function(color,message)val=string.format('<font color='..color..'>'..message..'</font>')return val end
        for _,in ipairs({
                {h,'Hour',h_Stand,lh_Stand,maxH,sH},
                {d,'Day',d_Stand,ld_Stand,maxD,sD},
                {m,'Month',m_Stand,lm_Stand,maxM,sM},
                {y,'Year',y_Stand,ly_Stand,maxY,sY}
                })do
            ------------------------------------------------------------------
            --print('t='..t[1]..' time='..t[2]..' x_Stand='..t[3]..' xl_Stand='..t[4]..' max='..t[5]..' total='..(stand+t[3]))
            if t[1]=='01'or t[1]=='00:01'or t[1]=='01 00:01'or t[1]=='01 01 00:01'then Debug('green','Reset '..t[2])
                total=stand+t[3] if stand==0 then standT=0 else standT=stand end self:SET(t[2],'print')
            else standT=stand+t[3] self:SET(t[2])
            end
            ------------------------------------------------------------------
            if standT<1000 then unit='L'
                if debug then Debug('grey',string.format('%s_Stand=%.0f-%.0f=%.0f '..unit,t[2],stand,t[3],standT))end
            else unit='M3' standT=tonumber(string.format('%.1f',(standT/1000)))
                if debug then s_total=((stand-standT)/1000)Debug('cyan',string.format('%s_Stand=%.0f-%.0f=%.0f '..unit,t[2],stand,standT,s_total))end
            end
            if t[4]<1000 then p_unit='L' last=t[4]else p_unit='M3'last=tonumber(string.format('%.1f',(t[4]/1000)))end
            new=t[2]..': '..standT..unit prev='prev_'..t[2]..': '..last..p_unit print(prev..' '..new)
            if t[4]>standT then i=low elseif t[4]<standT then i=high else i=equal end
            ------------------------------------------------------------------
            for _,in ipairs({{t[4],standT}})do
                --print(t[2]..' l_Stand='..v[1]..' > max='..t[5])
                if v[1]>t[5]then prev=c('red',v[1])else prev=c('lightgreen',v[1])end
                if v[2]>t[5]then new=c('red',v[2])else new=c('lightgreen',v[2])end
                info={'prev_'..t[2]..': ',prev..p_unit,' | ',t[2]..': ',new..unit..' '..i}style2(t[2]..'use')
            end
            ------------------------------------------------------------------
            if standT>t[5] and not t[6]then
                if t[2]=='Hour'then sH=true send=true end
                if t[2]=='Day'then sD=true send=true end
                if t[2]=='Month'then sM=true send=true end
                if t[2]=='Year'then sY=true send=true end
            elseif t[6]then
                if t[2]=='Hour'and t[1]=='01'then sH=false end
                if t[2]=='Day'and t[1]=='00:01'then sD=false end
                if t[2]=='Month'and t[1]=='01 00:01'then sM=false end
                if t[2]=='Year'and t[1]=='01 01 00:01'then sY=false end
            end
            if send then Message='Water use high in one-'..t[2]..'. Total of '..standT..unit..' where max='..t[5]telegram()Debug('red',Message)send=false end
            ------------------------------------------------------------------
        end
        setTable()
        Debug('green','-------------------------')
        ------------------------------------------------------------------
        info={c('cyan','Date: ')..c('yellow',datum)}style('lastupdate')
        info={c('cyan','Wifi: '..c('yellow',Wifi)..' dBm'),c('cyan','Factor: ')..c('yellow',factor),c('cyan','Counts: ')..c('yellow',count)}style('info')
        ------------------------------------------------------------------
        if debug then print('<div align="right">Wifi = '..Wifi..' Factor = '..factor..' Count = '..count..' Totaal = '..count..'</div>')end
        end,
        error=function(message)self:debug('error:', message)Message='ERROR: No water (Update)'bugError=bugError+1 Debug('red',Message..' error nr:'..bugError)
        if bugError>totalBugErrors then telegram()bugError=0 end end
        })
    end
    ------------------------------------------------------------------
    function QuickApp:refresh()self:getDeviceInfo()_f.setTimeout(self.pollingTime*1000,function()self:refresh()end)end
    ------------------------------------------------------------------
    function QuickApp:Update()self:onInit()end
    ------------------------------------------------------------------
    function QuickApp:onInit()Debug('green','HC3 start script at '.. os.date())self.http=net.HTTPClient({timeout=5000})self.pollingTime=60 self:refresh()end

     

    This part i think can be shorter then

     

    if standT>t[5] and not t[6]then
                if t[2]=='Hour'then sH=true send=true end
                if t[2]=='Day'then sD=true send=true end
                if t[2]=='Month'then sM=true send=true end
                if t[2]=='Year'then sY=true send=true end
            elseif t[6]then
                if t[2]=='Hour'and t[1]=='01'then sH=false end
                if t[2]=='Day'and t[1]=='00:01'then sD=false end
                if t[2]=='Month'and t[1]=='01 00:01'then sM=false end
                if t[2]=='Year'and t[1]=='01 01 00:01'then sY=false end
            end
            if send then Message='Water use high in one-'..t[2]..'. Total of '..standT..unit..' where max='..t[5]telegram()Debug('red',Message)send=false end

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Answer this question...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.

    ×
    ×
    • Create New...