Logic Machine Forum
While Loop for reading values - 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: While Loop for reading values (/showthread.php?tid=3091)



While Loop for reading values - tomnord - 06.01.2021

Hello.
I'm writing av script to read several values from different GA and comparing all of these values to get the highest of them as an output.
I can structure the GA's so that they come with increment of +1.
Any tips from people who have done this before?




-Regards


RE: While Loop for reading values - admin - 06.01.2021

Why not use tags? You can get values for all objects with a single function call this way.


While Loop for reading values - tomnord - 06.01.2021

Oh, I did not know.
Could you give an example?

Sent fra min SM-G980F via Tapatalk


RE: While Loop for reading values - admin - 06.01.2021

Code:
function tag_max(tag)
  local objects = grp.tag(tag)
  local result = -math.huge

  for _, object in ipairs(objects) do
    result = math.max(result, object.value)
  end

  return result
end

max = tag_max('my_tag_name')
log(max)



RE: While Loop for reading values - tomnord - 06.01.2021

works perfekt, thank you!


RE: While Loop for reading values - tomnord - 19.01.2021

Spinoff on this post.
Here we use tags to calculate the max value. Is there any clever way to disregard a value if a GA is "true"? I want to check if the damper is in faultstate before running the "max"script.


RE: While Loop for reading values - Daniel - 19.01.2021

This is an or gate, this
https://forum.logicmachine.net/showthread.php?tid=1851&pid=11545#pid11545
or this
https://forum.logicmachine.net/showthread.php?tid=291&pid=1518#pid1518


RE: While Loop for reading values - tomnord - 19.01.2021

i don't think that will work. There is one fault GA for each damper, and I only want to dismiss the value from that one damper. Is it possible to alter tags on GAs from script?


RE: While Loop for reading values - admin - 19.01.2021

Do you mean that you want to check another status object before including value in the calculation? In this case you need some addressing rules so you can get status object address for another tagged object.


RE: While Loop for reading values - tomnord - 19.01.2021

(19.01.2021, 13:30)admin Wrote: Do you mean that you want to check another status object before including value in the calculation? In this case you need some addressing rules so you can get status object address for another tagged object.

That is correct yes. If the Fault GA is "true" then the other value should be ignored.


RE: While Loop for reading values - admin - 19.01.2021

In this example each fault object has the next middle group (shift = 256, 1/1/123 => 1/2/123). Change as needed, shift can be negative.
Code:
function tag_max(tag, shift)
  local objects = grp.tag(tag)
  local result = -math.huge

  for _, object in ipairs(objects) do
    local fault = grp.getvalue(object.id + shift)
    if not fault then
      result = math.max(result, object.value)
    end
  end

  return result
end

max = tag_max('my_tag_name', 256)
log(max)



RE: While Loop for reading values - tomnord - 19.01.2021

Code:
input = event.getvalue()

if input == true then
  grp.removetags('Damper_1_Angle', 'Supply')
else
  grp.addtags('Damper_1_Angle', 'Supply')
end
This worked fine. This script is eventbased and used in all fault GA's. Will have to enter the Alias for each damper, but that is a one time thing.

Could this have been done easier?


RE: While Loop for reading values - Trond Hoyem - 25.01.2021

(19.01.2021, 15:00)tomnord Wrote:
Code:
input = event.getvalue()

if input == true then
  grp.removetags('Damper_1_Angle', 'Supply')
else
  grp.addtags('Damper_1_Angle', 'Supply')
end
This worked fine. This script is eventbased and used in all fault GA's. Will have to enter the Alias for each damper, but that is a one time thing.

Could this have been done easier?

Hi

Just to give my toughts....

I am doing smililar things as you describe here in many projects. What I have found to be the best way is to aloways use a very strict naming policy. By doing that I can find the address for fault (as you mention here) by replacin the code for temp with the code for fault.

To find the address I need I can then do something like this;
Code:
adresse = string.gsub(grp.find(event.dst).name, 'TRIGGER_OUTPUT', 'TRIGGER_INPUT')
LOCK = grp.getvalue('32/1/2')

if not LOCK then
  grp.write(adresse, event.getvalue())
end

Here I replace the TRIGGER_OUTPUT with TRIGGER_INPUT to find the address to forward the value to if the locking is not true.

You could do the same by simply replacing 'TEMPERATURE' with 'FAULT', or whatever would be your naming policy.