Logic Machine Forum
if condition not working - 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: if condition not working (/showthread.php?tid=5492)



if condition not working - mai - 02.07.2024

Why is this if condition not working? That is, even if some address is at '1', it always enters 'else'


if (grp.read('2/0/6')==1 or grp.read('2/0/8')==1 or grp.read('2/0/10')==1) then

  grp.write('12/0/100', 1) -- Zonas Comunes VA P0
 
  else
    grp.write('2/0/100', 0)
 
end


RE: if condition not working - admin - 02.07.2024

You need to use grp.getvalue() instead of grp.read(). Comparison with 1 won't work correctly because the value for 1-bit objects in true/false.

Assign a common tag to 2/0/6, 2/0/8 and 2/0/10 then map an event script to this tag:
Code:
value = grp.getvalue('2/0/6') or grp.getvalue('2/0/8') or grp.getvalue('2/0/10')
grp.write('2/0/100', value)

If you need multiple OR/AND gates then it's more convenient to use this solution:
https://kb.logicmachine.net/scripting/logic-functions-central-statuses/


RE: if condition not working - mai - 02.07.2024

(02.07.2024, 07:30)admin Wrote: You need to use grp.getvalue() instead of grp.read(). Comparison with 1 won't work correctly because the value for 1-bit objects in true/false.

Assign a common tag to 2/0/6, 2/0/8 and 2/0/10 then map an event script to this tag:
Code:
value = grp.getvalue('2/0/6') or grp.getvalue('2/0/8') or grp.getvalue('2/0/10')
grp.write('2/0/100', value)

If you need multiple OR/AND gates then it's more convenient to use this solution:
https://kb.logicmachine.net/scripting/logic-functions-central-statuses/

I have changed it to this way. In the log I do see that the variable takes the correct value, but it does not do the grp.write(), that is, it does not refresh the new value.


AZonasComunesP0 = grp.getvalue('2/0/6') or grp.getvalue('2/0/8') or grp.getvalue('2/0/10')
grp.write('2/0/100', AZonasComunesP0)
log(AZonasComunesP0)


RE: if condition not working - admin - 02.07.2024

Works for me. Make sure that 2/0/100 does not have the same tag as 2/0/6, 2/0/8 and 2/0/10. To lower the bus load use grp.checkwrite() instead of grp.write().


RE: if condition not working - mai - 02.07.2024

(02.07.2024, 07:53)admin Wrote: Works for me. Make sure that 2/0/100 does not have the same tag as 2/0/6, 2/0/8 and 2/0/10. To lower the bus load use grp.checkwrite() instead of grp.write().

yes it's working, thanks


RE: if condition not working - Bitver - 02.07.2024

(02.07.2024, 07:30)admin Wrote: You need to use grp.getvalue() instead of grp.read(). Comparison with 1 won't work correctly because the value for 1-bit objects in true/false.

Assign a common tag to 2/0/6, 2/0/8 and 2/0/10 then map an event script to this tag:
Code:
value = grp.getvalue('2/0/6') or grp.getvalue('2/0/8') or grp.getvalue('2/0/10')
grp.write('2/0/100', value)

If you need multiple OR/AND gates then it's more convenient to use this solution:
https://kb.logicmachine.net/scripting/logic-functions-central-statuses/

Quote:it's more convenient to use this solution

Why it is more convenient? It feels too overengineering.
Then, is it better to write ALL conversion logic of all features in project in such only one resident script?
Also about timers on that page, why not just use os.sleep with one of the new script execution policy?


RE: if condition not working - admin - 03.07.2024

There's a high chance of copy/paste errors when you have multiple scripts with the same logic but different input group addresses.
Event scripts can be used but it's easier to manage a single resident than tens of events.