Lua script

  • I have made a simple lua script to count pellet and show this on a virtuell counter in Domoticz. The problem is that the counter dont shows decimals. Can anyone tell me if it is some more i have to write in the script. Here it is
    counter = otherdevices_svalues['Counter']
    counter = tonumber(counter)

    commandArray = {}
    devicechanged['Pellets'] == 'On'
    counter = counter + 0.015
    commandArray['UpdateDevice'] = 82 .. '|0|' .. tostring(counter)

    return commandArray

  • Admin

    Try string.format("%.3f", counter)

    For example: commandArray['UpdateDevice'] = 82 .. '|0|' .. string.format("%.3f", counter)

  • Thanks but that didnt help.

  • Admin

    @Dragon I must have misunderstood what you are trying to accomplish. In lua 5.1.5, here is the result for what I thought you were trying to accomplish.

    0_1478209199557_Screen Shot 2016-11-03 at 2.39.40 PM.png

    Perhaps you should print(commandArray['UpdateDevice']) after you set it to confirm the value at that point. If the value of commandArray contains the string with decimals, as I suspect it will, then it must be something else or a Domoticoz issue. I also believe that tostring(counter) will include the decimals; the advantage of string.format() is that you can control the format of the resulting string...

  • I can try explain more how i made it. Sorry for my bad English, on my pellet boiler i have a switch that sends on to domoticz every turn with is 15 gram pellet. In domoticz is the switch called Pellets. Then i made a viruell counter in domoticz with name Counter. I want the lua script to + 0.015 to every turn the boiler do to Count how mutch pellet it use. It works now but shows without decimals like 2kg, 3kg and so. I can have so but it would be fun to se more exact weight.

  • What is your Device in DZ?

    I guess it is a Dummy but is it a general counter, switch etc?

  • @Dragon said:

    counter = otherdevices_svalues['Counter']


    counter = tonumber(counter)


    commandArray = {}
    devicechanged['Pellets'] == 'On'
    counter = counter + 0.015


    commandArray['UpdateDevice'] = 82 .. '|0|' .. tostring(counter)

    return commandArray
    put print after each change you do with counter to see what actually happen with your values, as @blacey wrote

  • Interesting projects

    I'm looking to create a counter that counts the number of times the temperature reaches above 30 degrees.
    Intend to use it when I bonfires

    Have you learned LUA on the internet?

  • @flopp In DZ the counter is incremental but i tried general to.

  • @flopp When i put in print(counter) it shows with decimals in the logg but the counter doesnt

  • @Affe I can not lua 😃 i read on lua pages on internet and experiment

  • This my script with decimals

    commandArray = {}
    if devicechanged['Panna_V'] or devicechanged['Huset_V'] then
        local panna_v = otherdevices_svalues["Panna_V"]
        local huset_v = otherdevices_svalues["Huset_V"]
        local hushall_kwh = uservariables['Hushåll_kWh']
        local huset_kwh = uservariables['Huset_kWh']
        local panna_kwh = uservariables['Panna_kWh']
        local panna, energy, huset, energy2
        _,_,panna, energy = string.find(panna_v, "(.+);(.+)")
        _,_,huset, energy2 = string.find(huset_v, "(.+);(.+)")
        --print ("energy2="..energy2)
        --print ("huset_kwh="..huset_kwh)
        husetdiff_kwh = energy2 - huset_kwh
        --print ("husetdiff kwh")
        --print (husetdiff_kwh)
        --print ("energy="
        --print ("panna_kwh="..panna_kwh)
        pannadiff_kwh = energy - panna_kwh
        --print ("pannadiff kwh")
        --print (pannadiff_kwh)
        hushal_kwh = husetdiff_kwh - pannadiff_kwh
        hushall_kwh = hushall_kwh + hushal_kwh
        --print ("hushåll_kwh")
        --print (hushall_kwh)
        panna = tonumber(panna)
        huset = tonumber(huset)
        hush = huset - panna
        ener = energy2 - energy
        if hush > 0 then
            commandArray['Variable:Hushåll_kWh']= tostring(hushall_kwh)
            commandArray['Variable:Huset_kWh']= tostring(energy2)
            commandArray['Variable:Panna_kWh']= tostring(energy)
            abc = tostring(hush)
            def = tostring(hushall_kwh)
            commandArray['UpdateDevice'] = '272|0|'';'..def..''
    return commandArray

    I am using Dummy, Electric(Instant&Counter) version 3.5721 of DZ

    Panna_V(heating system) and Huset_V(whole house) is electric usage meters that report every 5 minute. I use my Dummy to show my electric consumption without(minus) my heating system

Log in to reply

Suggested Topics

  • 10
  • 10
  • 1
  • 5
  • 13
  • 6
  • 11
  • 5