Automatic Function

Is there a function to periodically check the value of tag and take action? I'm looking for a function that automatically changes the color of buttons, text, etc. when the value of tag exceeds a certain level.

This is usually accomplished via binding tag values to component properties.

Thank you. I want to write a script that uses the script editor of Property Change to change the color when the tag value is 0, and I wrote the following, but an error appears. I'm sorry about the basics of python, but if you know what the cause is, it would be helpful if you could tell me.

a=system.tag.readBlocking(["tag path"])[0]
values=a.value
if values == 0:

The error message is:
Parse error for event handler "propertyChange" SyntaxError: ("mismatched input '\n' expecting INDENT", ('', 3, 15, 'if values == 0:\n'))

Am I writing the script wrong in the first place? I want to write a script that changes the color of the text field every time the value of the tag changes, so I write it in the script editor of the property change of that text field.

Python relies on proper and consistent indenting after any control statement ending in a colon (:). For us to be able to help you need to post your code using 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 πŸ–‰ below your post so you can fix it.
i
In any case, a better construction would be,

if not values:       # checks for values == 0.

But as explained by @witman, this can be done easily using an expression binding on the color property of a component.

https://docs.inductiveautomation.com/display/DOC81/Expression+Bindings+in+Perspective

Use the expression binding to get the controlling value. Then use a map transform in that binding to set the color.

https://docs.inductiveautomation.com/display/DOC81/Map+Transform

Thank you for your advice. I want to bound tag to background and set it so that the color changes when it reaches the specified value like a reference link, but I can't edit the color or value of property.

Also, I can edit it in the tag part, but I cannot enter a decimal point etc. in the Value part.

As a result of various investigations, the method that I was told seems to be the method of doing it with "view". I use "vision", how do I do it with "vision"?

You can edit your original post to add the Vision tag to avoid confusion with Perspective. This will help attract the experts.

I would use an expression binding for this.
Example:
if ({[default]TestTags/FloatTag.value} > 1.5, '0,0,0', '255,255,255')
The if statements can be nested many times to create complex and dynamic bindings

But don't nest ifs if the conditions share the same 'data source', use switch or case instead.
for example, don't right

if ({someTag} = 1, "one",
    if ({someTag} = 2, "two",
        if ({someTag} = 3, "three",
            "default"
        )
    )
)

do this instead:

case({someTag},
    1, "one",
    2, "two",
    3, "three",
    "default"
)

Obviously this doesn't work with completely/unrelated conditions, or with greater than/less than comparisons.

3 Likes

Edit: It seems I didn't read your answer fully. Disregard this post.

No. Read the last line of Pascal's post again.

https://docs.inductiveautomation.com/display/DOC81/switch

1 Like

I had already corrected myself before you posted this. :sweat_smile:

I would bind the tag to a custom property, then use the style customizer.
https://docs.inductiveautomation.com/display/DOC81/Vision+Component+Customizers#VisionComponentCustomizers-StyleCustomizer

4 Likes

Aside from the style customizer, which is a Vision thing, the key to the question is to use an expression binding to translate your tag values into a range of colors or indices for colors. Whether Vision or Perspective. I highly recommend you bind the source tag to a custom property (perhaps indirectly), and then use that property in your expressions or styling.

Note that one "pulls" the tag into the UI for evaluation--you don't "push" from the tag to the UI. That is, don't try to do this sort of thing with a tag event or in a context/scope outside the UI.

3 Likes

Thank you all for your advice. If I used the style customizer, I could use a component that directly reflected the value of a tag such as display, but I could not change the color depending on the value of the tag with a label etc. I want to change the color of the tag value with conditions such as < instead of =.

A simple expression binding on your background color property could accomplish this using a custom property bound to your tag:
image

Using @pascal.fragnoud 's example as a template, your binding would look something like this:

if ({Root Container.Button.myCustomProp} < 1.5, '100,100,100',
    if ({Root Container.Button.myCustomProp} < 2.5, '200,200,200',
        if ({Root Container.Button.myCustomProp} < 3.5, '255,255,255',
            '0,0,0'
        )
    )
)

image

Here is the result when applied to the background color property of a button:
image
image
image
image

Done! Thank you so much! Thank you for all the advice!

1 Like

Love it or hate it, I much prefer keeping the ifs on the same line to help with reading as well as horizontal space. It reads more like a case statement then as well which keeps it more consistent:

if ({Root Container.Button.myCustomProp} < 1.5, '100,100,100',
if ({Root Container.Button.myCustomProp} < 2.5, '200,200,200',
if ({Root Container.Button.myCustomProp} < 3.5, '255,255,255',
'0,0,0')))

:person_shrugging:

1 Like