Toggle a boolean PLC tag via script

Please forgive the newbie question!!

I have created a two state rotary switch (Auto/Man, Start/Stop) and am trying to get the syntax correct for toggling a Boolean tag in the processor. The script as written is:

system.tag.read (“Intake/TEST_TOGGLE”)

if “Intake/TEST_TOGGLE” == 1:
system.tag.write(“Intake/TEST_TOGGLE”, 0)
else:
system.tag.write(“Intake/TEST_TOGGLE”, 1)

It will set the tag value to “1” if it is a “0”, but won’t reset the bit to “0” if it is a “1”

There is a part two to this question, but I’ll hold off until I have part one answered!!

Thanks!

I found a solution to this. Changed the script to:

x = system.tag.read (“Intake/TEST_TOGGLE”)

if x.value is True:
system.tag.write(“Intake/TEST_TOGGLE”, 0)
else:
system.tag.write(“Intake/TEST_TOGGLE”, 1)

It works perfectly!

1 Like

:thumb_left:

I know this thread is very old, but come with the same problem today. Why is it not possible to do:

system.tag.write( “Intake/TEST_TOGGLE”, not system.tag.read (“Intake/TEST_TOGGLE”))

I tried and it just not work.

Try this:

system.tag.write("Intake/TEST_TOGGLE", not system.tag.read("Intake/TEST_TOGGLE").value)

system.tag.read returns a QualifiedValue object; you need to pull the value out.

2 Likes