Boolean set/reset in perspective script

It might be silly question, but I encounter issue with set/reset the boolean bit in perspective scripting.
for example, I want to use toggle button template.
in a new view, I created the following params
image
then in the button event, i have the following script.
self.view.params.tagValue = not self.view.params.tagValue
it works.
But if I change to the following script, it won’t work. It looks like the value has been changed to string type.

def runAction(self, event):
        currentValue = self.view.params.tagValue
	if currentValue:
		self.view.params.tagValue = "false"
	else:
		self.view.params.tagValue = "true"

If I change it to the following, the datatype is changed to numeric, still not working.

def runAction(self, event):`
	currentValue = self.view.params.tagValue
	if currentValue:
		self.view.params.tagValue = 0
	else:
		self.view.params.tagValue = 1

If I change it to the following scripting, it does not even pass the syntax check.
If I change it to the following, the datatype is changed to numeric, still not working.

def runAction(self, event):
	currentValue = self.view.params.tagValue
	if currentValue:
		self.view.params.tagValue = false
	else:
		self.view.params.tagValue = true

How do I assign boolean data value in perspective script?

It also happened when I read/write the PLC boolean data type, I need to use data transform to transform “true/false” to “0/1” before I can read the value correctly on the view.

Did I missed something? Or is there any way to fix the perspective params to certain data type?

Many thanks in advance.

Tip: use the </> code formatting button to preserve code indentation and apply syntax highlighting. It’s essential for Python and makes any code much easier to read. There’s an edit button (pencil icon) below your post so you can fix it.

1 Like

self.view.params.tagValue = “false”
should be
self.view.params.tagValue = False

"false" is a string.
False is a boolean. In Python it’s True / False and None (null). Capitals matter!


Note that in Ignition’s Expression Language that case doesn’t matter. See Expression Language and Syntax - Ignition User Manual 8.1 - Ignition Documentation.

4 Likes

Probably easier to do

self.view.params.tagValue = not self.view.params.tagValue
2 Likes

Thank you for the reminder.

It works after applying Cap Letter.

Script needs follow Python syntax. But Expression does not. Easy to get confused.

The property editor also uses lowercase which I feel is a major contributing factor to this common error.

Good.
Don’t forget to fix the code formatting in your original question. It might help someone else in the future.

No winning there. The property editor displays JSON. Java and Javascript (and thus JSON, Javascript Object Notation) use true and false. No matter what we pick, it’s going to confuse someone.

4 Likes

applied the formatting button, but the indentation was not preserved.

You seem to have applied it line by line which results in a each line enclosed in single backticks. Select a whole block of code and press the </> button. You should get a result like

    ```
    <code goes here>
    ```
1 Like

You can even tell it what language to use for syntax highlighting:

```python
<code goes here>
```

Side note, I’ve gotten used to start scripts in the script console with

false = False
true = True

So I don’t have surprises when I paste in some json from a component’s property.

1 Like

Oy! But the next guy is toast.

No one else is going to use my script console. I’d bite if someone tried to touch my keyboard.

2 Likes