Need help with expression tag returns tag value if hour is correct
if{[.]hour}=15,{[.]Crank_max,0}
See this doc,
https://docs.inductiveautomation.com/display/DOC80/if
Probably something like,
if({[.]hour}=15,{[.]Crank_max},0)
is there a way that I can just return the true value? I just want this to happen one time when the hour is15, I just want to store the crank_max value in that tag. like a one shot
Not with an expression, which must always return a value, by definition.
If you want to do something as a 'side effect' of a tag changing, you want a script. Potentially a tag event script on hour
, where every time it changes, if the hour == 15, it will write the value of Crank_max
to another tag.
yes, what does that look like?
Something like:
if currentValue.value == 15
crankMax = system.tag.readBlocking(["[.]Crank_max"])[0].value
system.tag.writeBlocking(["[.]SomeMemoryTag"], [crankMax])
Assuming all your tags are colocated in the same folder.
could I do that for multiple values in the same script? currentValue.value == 5, ==1, == 9.
Just duplicate the script using the same["[.]SomeMemoryTag"]. I want assign the value of Crank_max when the hour = 5 or 1 or 9 to[ "[.]SomeMemoryTag"]
Yes, either use:
if currentValue.value == 15 or currentValue.value == 9 or currentValue.value == 1
(and so on)
or
put your conditions into a data structure like a list and check for membership with in
:
hoursToCheck = [1, 5, 9, 15]
if currentValue.value in hoursToCheck:
crankMax = system.tag.readBlocking(["[.]Crank_max"])[0].value
system.tag.writeBlocking(["[.]SomeMemoryTag"], [crankMax])
do I leave this line or remove it?
def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
That line is not removable. All scripting underneath that line needs to be indented one tab.
still getting scripting error expecting colon -
def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
if currentValue.value == 5 or currentValue == 13 or currentValue.value == 21 or currentValue.value == 8
crank_max = system.tag.readBlocking( ["[.]Crank_max"]) [0].value
system.tag.writeBlocking( ["[.]P1_Target"], [crank_max])
Please use the "preformatted text" button in the comment editor to style your pasted code so we can read it.
def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
if currentValue.value == 5 or currentValue == 13 or currentValue.value == 21 or currentValue.value == 8
crank_max = system.tag.readBlocking(["[.]Crank_max"])[0].value
system.tag.writeBlocking(["[.]P1_Target"], [crank_max])
forgot to add the .value on second line but still getting same error
You are missing a colon on the end of your if
statement. Common python mistake that the error message was trying to tell you.
Thanks Phil, don't do a lot of scripting. Is there examples on how to properly write these scripts
Online python tutorials would be best. But be aware the CPython 2.7 is long-dead, while Jython 2.7 is current (for a number of reasons, mostly good). When looking at online docs for v2.7, you have to ignore the big, colorful warning banner about the old version.