Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Troubleshooting
  3. Lua script

Lua script

Scheduled Pinned Locked Moved Troubleshooting
12 Posts 4 Posters 5.6k Views 4 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dragon
    wrote on last edited by Dragon
    #1

    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 = {}
    if
    devicechanged['Pellets'] == 'On'
    then
    counter = counter + 0.015
    commandArray['UpdateDevice'] = 82 .. '|0|' .. tostring(counter)
    end

    return commandArray

    F 1 Reply Last reply
    0
    • blaceyB Offline
      blaceyB Offline
      blacey
      Admin
      wrote on last edited by
      #2

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

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

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Dragon
        wrote on last edited by
        #3

        Thanks but that didnt help.

        blaceyB 1 Reply Last reply
        0
        • D Dragon

          Thanks but that didnt help.

          blaceyB Offline
          blaceyB Offline
          blacey
          Admin
          wrote on last edited by blacey
          #4

          @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...

          1 Reply Last reply
          1
          • D Offline
            D Offline
            Dragon
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • F Offline
              F Offline
              flopp
              wrote on last edited by
              #6

              What is your Device in DZ?

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

              D 1 Reply Last reply
              0
              • D Dragon

                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 = {}
                if
                devicechanged['Pellets'] == 'On'
                then
                counter = counter + 0.015
                commandArray['UpdateDevice'] = 82 .. '|0|' .. tostring(counter)
                end

                return commandArray

                F Offline
                F Offline
                flopp
                wrote on last edited by flopp
                #7

                @Dragon said:

                counter = otherdevices_svalues['Counter']

                print(counter)

                counter = tonumber(counter)

                print(counter)

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

                print(counter)

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

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

                D 1 Reply Last reply
                0
                • A Offline
                  A Offline
                  Affe
                  wrote on last edited by
                  #8

                  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?

                  D 1 Reply Last reply
                  0
                  • F flopp

                    What is your Device in DZ?

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

                    D Offline
                    D Offline
                    Dragon
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    0
                    • F flopp

                      @Dragon said:

                      counter = otherdevices_svalues['Counter']

                      print(counter)

                      counter = tonumber(counter)

                      print(counter)

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

                      print(counter)

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

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

                      D Offline
                      D Offline
                      Dragon
                      wrote on last edited by
                      #10

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

                      1 Reply Last reply
                      0
                      • A Affe

                        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?

                        D Offline
                        D Offline
                        Dragon
                        wrote on last edited by
                        #11

                        @Affe I can not lua :smiley: i read on lua pages on internet and experiment

                        1 Reply Last reply
                        0
                        • F Offline
                          F Offline
                          flopp
                          wrote on last edited by
                          #12

                          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
                              --print(pan)
                              --print(hus)
                              
                              _,_,panna, energy = string.find(panna_v, "(.+);(.+)")
                              _,_,huset, energy2 = string.find(huset_v, "(.+);(.+)")
                              --print(panna)
                              --print(huset)
                              --print(energy)
                              --print ("energy2="..energy2)
                              --print ("huset_kwh="..huset_kwh)
                              husetdiff_kwh = energy2 - huset_kwh
                              --print ("husetdiff kwh")
                              --print (husetdiff_kwh)
                              
                              --print ("energy="..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
                              
                              --print(hush)
                              --print(ener)
                              if hush > 0 then
                              
                                  --print(el)
                                  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|'..abc..';'..def..''
                              else
                              end
                              
                          end
                          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

                          1 Reply Last reply
                          1
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          18

                          Online

                          11.7k

                          Users

                          11.2k

                          Topics

                          113.1k

                          Posts


                          Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • MySensors
                          • OpenHardware.io
                          • Categories
                          • Recent
                          • Tags
                          • Popular