This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

OR LOGIC VIA TAG
#6
Hello admin,

Can the part below be edit to be able to calculate with a resolution of 1 decimal point the average value of 2byte float or any byte value (temp, brightness, power ...)? Now in the script says the value must be between 0-100.

Code:
-- AVERAGE value
  calc['avg'] = function(group)
    local result, count, value = 0, 0

    for _, address in ipairs(group.objects) do
      value = values[ address ]

      -- number must be in [0..100] range!!!!!!!!!!!!!!!!!
      if type(value) == 'number' then
        result = result + value
        count = count + 1
      -- boolean true is 100%, false is 0%
      elseif type(value) == 'boolean' then
        if toboolean(value) then
          result = result + 100
        end

        count = count + 1
      end
    end

    if count > 0 then
      result = math.floor(result / count + 0.5)
      grp.checkwrite(group.output, result)
    end
  end

Thank you in advance

(06.04.2022, 20:49)manos@dynamitec Wrote: Hello admin,

Can the part below be edit to be able to calculate with a resolution of 1 decimal point the average value of 2byte float or any byte value (temp, brightness, power ...)? Now in the script says the value must be between 0-100.

Code:
-- AVERAGE value
  calc['avg'] = function(group)
    local result, count, value = 0, 0

    for _, address in ipairs(group.objects) do
      value = values[ address ]

      -- number must be in [0..100] range!!!!!!!!!!!!!!!!!
      if type(value) == 'number' then
        result = result + value
        count = count + 1
      -- boolean true is 100%, false is 0%
      elseif type(value) == 'boolean' then
        if toboolean(value) then
          result = result + 100
        end

        count = count + 1
      end
    end

    if count > 0 then
      result = math.floor(result / count + 0.5)
      grp.checkwrite(group.output, result)
    end
  end

Thank you in advance


I guess just removing math.floor(..........+0.5) is the solution to this?
Reply


Messages In This Thread
OR LOGIC VIA TAG - by RafalBor - 26.06.2021, 13:23
RE: OR LOGIC VIA TAG - by admin - 26.06.2021, 13:26
RE: OR LOGIC VIA TAG - by RafalBor - 26.06.2021, 13:45
RE: OR LOGIC VIA TAG - by RafalBor - 26.06.2021, 14:46
RE: OR LOGIC VIA TAG - by admin - 28.06.2021, 06:28
RE: OR LOGIC VIA TAG - by manos@dynamitec - 06.04.2022, 20:49
RE: OR LOGIC VIA TAG - by admin - 07.04.2022, 06:53
RE: OR LOGIC VIA TAG - by manos@dynamitec - 07.04.2022, 13:12

Forum Jump: