Logic Machine Forum
Change Unit/Suffix automatic - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Change Unit/Suffix automatic (/showthread.php?tid=4183)



Change Unit/Suffix automatic - victor.back - 09.08.2022

Hi.



Is it possible to change this Unit/Suffix automatic it the value is over a specific number?
I run this script in resident and have it to calculate the number thats presented, but then I also need to change Unit/Suffix from kWh to MWh so theres no confusion : )

Is this possible to implement in my script or can´t this be done?

Code:
-- Total power year
r1, r2, r3, r4 = mb:readregisters(505, 4)
if r1 then
  value = r1 * 0x1000000000000 + r2 * 0x100000000 + r3 * 0x10000 + r4
  currentvalue = grp.getvalue('32/1/54')
  if tonumber(currentvalue) ~= value then
    if value >999
      then
      grp.update('32/1/54', value /1000000)
      else
        grp.update('32/1/54', value /1000)
    end
  end
end



RE: Change Unit/Suffix automatic - Daniel - 09.08.2022

Use this https://openrb.com/docs/lua.htm#grp.create


RE: Change Unit/Suffix automatic - admin - 09.08.2022

Changing units via grp.create will require UI reload for new units to appear.
You can use string data type instead of floating point for value display:
Code:
value = r1 * 0x1000000000000 + r2 * 0x100000000 + r3 * 0x10000 + r4

if value > 999 then
  units = 'MWh'
  value = value / 1000000
else
  units = 'KWh'
  value = value / 1000
end

value = string.format('%.3f', value) .. units
grp.checkupdate('32/1/54', value)



RE: Change Unit/Suffix automatic - victor.back - 09.08.2022

(09.08.2022, 07:49)Daniel Wrote: Use this https://openrb.com/docs/lua.htm#grp.create

Nice thanks.
Do I have to make a new object by using grp.create for me to be able to change the units? Or can I somehow just change the units in my already created object thats not created by grp.create?


RE: Change Unit/Suffix automatic - admin - 09.08.2022

grp.create can also update existing objects' properties. But as I've said you won't see this changes in the visualization unless you do a full reload.


RE: Change Unit/Suffix automatic - victor.back - 09.08.2022

(09.08.2022, 08:33)admin Wrote: grp.create can also update existing objects' properties. But as I've said you won't see this changes in the visualization unless you do a full reload.

Oh did´nt see your reply.
I implemented your script, and it work nice. thanks for the help.

(09.08.2022, 08:14)admin Wrote: Changing units via grp.create will require UI reload for new units to appear.
You can use string data type instead of floating point for value display:
Code:
value = r1 * 0x1000000000000 + r2 * 0x100000000 + r3 * 0x10000 + r4

if value > 999 then
  units = 'MWh'
  value = value / 1000000
else
  units = 'KWh'
  value = value / 1000
end

value = string.format('%.3f', value) .. units
grp.checkupdate('32/1/54', value)
I use this one and gets the string presented with  4.564MWh.

I can change the presentation to 4.56MWh by change '%.3f' to '%.2f'
But how do I change this presentation as the number gets bigger?
So when the number gets bigger it also gets lesser decimals.