Ignition is an incredibly powerful and modern SCADA Software, and one of the things that makes it so powerful is the ability to write code in the world's easiest programming language, Python!
TIMESTAMPS: 00:00 Intro and Setup 01:53 Writing Python in the Ignition Script Console 06:08 Writing Python in Ignition Tag Value Changed Scripting 11:45 PCBWay Sponsorship 12:25 Writing Python in Ignition Gateway Events 16:04 Writing Python in Ignition Project Scripts 19:45 Outro and Miscellaneous
hello and welcome back to the channel in
today's video I'm going to show you how
to write python scripting inside of the
SK platform ignition I'm going to show
you how to do it in three different
places and we are going to cover a lot
of material so let's get right into it
today we're going to talk about one of
the things that makes ignition so
incredibly powerful and uh super easy
for traditional programmers to break
into the world of industrial controls
and automation but also for controls and
automation Engineers to learn a little
bit more more of the conventional
programming skills so to speak and part
of the reason for that is you can write
uh code in Python and so uh to do this
I'm going to actually save us a little
bit of trouble of watching me scrap
together a piece of python code but I'm
going to propose a scenario for you guys
um and hopefully you can see in the
bottom left here I have a tag that I've
created called input array okay and it
is just an integer array so there are 10
integers inside of this uh array and
that is all that's in there and we can
you know just manually remove rows if we
want to it's it's just a memory tag it's
a block of integers but let's say I want
to write a script that's going to
dynamically read through uh that integer
array look at how many elements are
inside of that integer array and create
a tag for each of those elements
individually this could be useful for
trending like creating historical graphs
of temperatures and flows and things
like that um or a variety of purposes so
there's a lot there are a lot of reasons
you might want uh to do this inside of
python rather than break it out uh when
you're like reading a block of data from
a PLC for example you might read that in
uh as an array so let's take a look now
uh I'm assuming you have a reason you
want to write python ignition so let's
take a look at uh first example of just
how to test a script while writing it in
Python so your best friend as a
developer when you're inside of ignition
designer and you want to test some
python script is the script console so
if you go to tools at the top and then
script console it's going to open a
window that looks kind of like a very
Bare Bones IDE um in a conventional like
programming setting like a Jupiter
notebooks or something um none of the
bells and whistles of like a pie charm
or vs code but the idea is you write
code on the left and it gives you output
on the right so I'll talk you through
the code that I've written already and
hopefully you can tell if you're
somewhat familiar with the python syntax
that other than knowing some specific
ignition syntax in here uh the code
really reads like python it's a variable
set equal to a value it's uh 4 I in
range whatever try except 4 I in range
right like this is python code with some
ignition specific syntax you'll just
have to refer to ignition forums ask a
friend ask the internet ask ignition
support um if you don't know the
specific syntax of the thing that you're
trying to do but basically this is a
script to take elements
of an array and break them into child
tags okay um so there are a lot of ways
to do this this script is in no way the
only way you could do this sort of
functionality but I start by reading the
raw data from from the input array tag
and then uh I Define the path where I'm
going to store and delete any child tags
so the first thing I do and I'm kind of
using a fudge Factor here of 20 but
first thing I'm going to do is clear out
any tags that might already be in there
so if I've already um done this script
previously and there were let's say six
array elements already sitting in there
this code will go through and it will
delete those all out and then what it's
going to do is 4 I in range length of
the data so in the length of the array
it's going to define a new tag uh that
will be called array element 0 1 2 3 4 5
to whatever length we have and then it's
going to configure the tag meaning
create the tag uh and then it will write
a value to that tag of whatever's in
that array so is this a somewhat
confusing example I hope not um but
remember I have an array here that has
the values in it of uh 10 20 30 40 50
and 60 so our goal is to create a and
you can see there's no drop down in the
child tags folder currently our goal is
to write a python script that is going
to read the data from that tag and then
it's going to create six children tags
that say 10 20 30 40 50 60 that should
be what this code does and so if I
execute it you'll see I actually get bad
knot found which is completely fine
because that's the delete code not
finding the tags and then all of the
goods are the configuration and the
right blocking working so if we click on
child tags over on the left now you'll
see we've dynamically created array
element 0 1 2 3 four five six uh just
zero through five um which is cool and
their values are 10 through 60 which
makes intuitive sense but now let's pop
into our array and let's say between 10
and uh between 30 and 40 we have 35
right and after 60 we have 70 okay so
let's say that the array has just been
updated and now there's eight values and
what you'll see is the script in the
script console isn't going to uh
automatically update it we have to hit
execute again but when we hit execute
again the same code works and it gives
us 10 20 30 35 40 50 60 and 70 okay so
this is some very basic code to generate
tags as you rip them out of an array
dynamically as well as clear out
Children tags that may have already been
in there this is pretty cool but I think
what you guys are probably noticing
already and it's no big deal if not um
but is it's not super convenient to have
to manually execute a script every time
an array updates and so what we want to
do is explore how to get that code
firing on tag change and that's what
we're going to talk about now because
there are a lot of places that you can
embed this script that we've just
written uh into a different location
that will do some of that functionality
for you automatically so let's take a
look at the first one and I'm not going
to lie we are pretty much immediately
going to look at better places to store
this scripting but if you have simple
python functionality that you want to do
you can double click on a tag or right
click and hit edit tags and then there
is an option on the left for scripting
and for every tag value change and
quality change are going to be two of
your options as well as alarm events but
we're not going to talk about alarms in
today's video and quality change means
like if you're reading values off of a
PLC and the quality goes from good
healthy I can see this tag to oh bad oh
crap I can't see the tag uh you could do
something here but what we're going to
do is value change and when the value of
an array changes that means two things
either we've changed one of the integers
inside of the array or we've changed
something about the array itself like
the overall dimensions
so we're going to drop the code that we
just wrote inside of this value change
location and what you can probably see
uh just by looking at kind of what's
available to you here they give you some
values available to you just by the
nature of it being a value change script
so you could use the tag path of this
tag the tag itself the previous value or
the current value in this tag change
script so theoretically rather than
reading the value of this tag we could
just use current value that's a little
bit of ignition specific syntax if you
have questions about that let me know in
the comments below and we can talk about
it more but I'm actually going to leave
the code exactly as is just to
illustrate that it works exactly as is
um and then one thing I'm going to add
here is I'm going to add a logger and
this is basically ignitions version of
like a web hosted hosted by their
Gateway console window so think of the
print statement in a conventional python
script logger is like ignition's way of
giving you that same functionality um so
if you do logger equals and then the
specific syntax is system.
u.get logger just like that and then you
get to define a name so we will call
ours um Python tutorial okay I love
python
tutorials all right and then what we're
going to do is loger doino and there are
a few classes of uh logger dot you could
do logger warn debug uh info there might
be more but um Trace I think is one but
basically I'm just going to do logger
dotinfo which is the easiest one and
we'll just say this came from a tag
change script okay with an exclamation
mark we'll drop it right there and all
this should do is when we update this if
the script works this should appear in
the log on the Gateway and I'll show you
how to get there in just a second but
for now let's hit okay and uh let's hit
save on our project and nothing happens
because the array still has the value it
already had but let's pop in here and
let's
add 80 90 and just to mess with the
people with OCD let's make this one
1,000 okay and let's hit apply and okay
and you can see the array elements are
already there and they're there in the
order that we defined in the previous
scripting and they're already populated
0 to 10 with all 11 elements so now if I
pop back into the array uh and I delete
let's say 1,000 all the way up to 30 so
there's just these two rows in there 10
and 20 and I hit apply again and I hit
okay all right so just by putting in a
value change script this code is working
and now if I go to my Gateway right if I
go to my host here uh and disregard
everything before like
23114 because that was me testing and
setting up for this tutorial um but what
you can see is Python tutorial right the
logger name that I just gave uh to this
logger is saying hey this message came
from a tag script this came from a tag
script this came from a tag script so
that represents the three times the
script ran it's logging it at each time
and you get a timestamp of when it
happened you get here when you're on the
Gateway and you're at home you go to
status and then you go to logs and this
is absolutely going to be an ignition
developer's best friend especially if
you're trying to set up python scripting
and you're getting weird errors and
you're not sure how to track it down uh
you're going to want to rely on logging
a lot of information okay so I think I
kind of alluded to already uh not
alluded to I directly said it we're
going to move this this is not
necessarily the best spot to uh have
this kind of scripting live it is
absolutely acceptable and if you have
smaller applications or you're the sole
developer for a plant put the code where
you want to put the code but in the
instance of wanting to store the code
somewhere where it's maybe easier to
share easier to immediately see all of
the scripts that are executing in a
project uh you want to keep it elsewhere
I think now is a perfect time to take a
quick pause and talk to you about the
sponsor of today's video PCB way PCB way
is a long-term supporter of the L Tech
Channel and I love working with them
because their products and services are
all about empowering makers as their
name suggests the core of their business
is manufacturing pcbs or printed circuit
boards this is such a cool way for
hobbyists and makers to turn their
electronic designs into Super
highquality and professional grade
circuit boards beyond that they also
have the ability to support all your
prototyping needs with highquality 3D
printing and metal fabrication as well
be sure to check them out at the
description below this video and thank
you so much to PCB way for sponsoring
the video so what I'm going to do I'm
not going to delete this right now cuz
you never know maybe want to turn it
back on I'm just going to uncheck this
enabled box and so now what you'll see
if I uh update my array okay so let's
just add 30 and 40 back in here 30 40
okay and I hit apply and I hit apply and
I hit okay and I hit save you'll see the
array elements do not update and that is
because we disabled the script that is
on there so if you go to scripting you
go to Value change it is not enabled
okay so let's look at the better place
to put it and I already have it
highlighted actually but in the uh
project browser in the top left if you
go to scripting there is Gateway events
as well as project Library let's start
with Gateway events okay and you can see
these ones over on the left they all
have utility we'll probably get to a
tutorial on almost all of them
eventually but for now let's click on
tag change okay and what we're going to
do is we're going to call this uh array
breakout all right that's what we'll
call this script and uh ch change
triggers I almost always turn off
quality and timestamp because I want to
trigger on the value changing of this
tag so I'm going to grab the path of
this input array tag by right clicking
and going down to copy path and then in
my Gateway events here I'm just going to
drop the tag path here now you don't
need to copy path like that you can just
hit your uh tag and if you have a small
system you're probably able to uh just
go right to it and find it pretty quick
just like that but you can see that's
just the same way a second way of
getting the same data okay so we have
the code in here and now we need to put
our script in here so I'm going to go in
here and copy the entire script okay and
then back to my Gateway events and I'm
going to drop that code in it always
does this funky indentation stuff but
don't worry about it it's the exact same
code the only thing I'm going to do is
I'm going to change from logger dotinfo
I'm going to change it to this came from
a Gateway script okay so a Gateway event
script now I'm going to hit apply and
I'm going hit okay and I'm going to hit
save which sends it live and you can see
it update immediately right there's 10
20 30 40 we'll go back in We'll add 50
just so you guys can see I'm not
crazy um well I'm crazy but not that way
okay so we already have tag 50 which is
awesome and if we go back to the logs
you'll see this came from a gateway
event script okay so we had those tag
change scripts now we have our Gateway
event scripts so that is a super useful
uh thing to know a thing to store and
then also useful to know excuse me tag
value change scripts are obviously
living on the tag themselves so that's
on the tag provider in the tag browser
this Gateway event is going to be
executed at the Gateway level meaning
it's using Gateway resources uh and is
not project specific so something that
we can do is we can make a project
library and this is just another place
to store script um honestly I'm not here
to tell you which one of these is best
and worst like if you prefer the code
living on the tag that's firing it
that's honestly fine um as you get to
larger systems and you maybe have
hundreds of tag change scripts it might
be very useful for you to see all of
those in one spot um but as long as
you're documenting where everything
lives it it's totally fine to just mix
and match try a little bit of everything
um but let's go ahead and copy this
script and what I'm going to do is I'm
going to put it in a new location and so
I'm going to go into now I'm in
scripting and I am in Project library
and I'm going to create a new script and
I will call this array breakout okay and
in here just like in a conventional uh
python IDE I'm going to create a
function I'll call it run and I'll put
all of this code inside of it it but now
I'm going to have in the logger I'm
going to say this came from a project
stored script okay so I'm going to save
this file array breakout um and first
thing easiest thing is if I go back into
Gateway events and I uh tags and I
disable this one so that it's not firing
for now first thing you'll be able to
see is if I go in here and I add uh
let's say now that this is 60
okay then uh you'll see the thing
doesn't update automatically right but
if I were to go back to the script
console I actually don't know if this
will work so let's just see if I were to
import array breakout which is the file
that those work in uh and let's say as
AB so I have to type that and I just do
ab. run like that and if I hit execute
on this execute okay it actually worked
we got 60
um and so the script console is able to
take that command that we just stored in
a file and execute it and I think if we
go to the logs you can see that actually
that one uh didn't store this came from
a Gateway event script did
it this came from a project yeah um
which is actually a feature of the
script console if you're um sort of new
to scope in uh ignition just know that
script console is a local thing and it's
not hitting the Gateway and so when you
are messing around in here you can't use
the logger but you could use uh this
interactive interpreter on the right
side so you could swap out logger
statements for print statements and they
would show up on the right but they
would not appear in your uh Gateway logs
okay but let's go one step further
basically the final step and show how
you might want to use this rather than
as this code gets longer and longer and
more convoluted rather than having all
that code live here you could do this
you could call the script that lives in
the array breakout folder and you could
just tell it to run when this tag value
changes so I'm going to enable that I'm
going to say okay and say all right and
now I'm going to save everything and I'm
going to go into the array and I am
going to add a bunch more tags and we'll
say this is 63 66
7075 80 okay so I add a bunch more tags
I hit okay and what you'll see is we get
all those tags we get 63 66 70 75 80 um
and what just happened is this root tag
input array changed we triggered a
Gateway event for tag change script that
then called a script that we stored in
Array breakout and it ran this file is
that convoluted you bet was that the
best way I could think of showing you
all the different places and ways that
you could write python also yes okay and
so if you look at uh the logs after a
test like that you'll see now we have
this came from a project stored script
this came from a project stored script
um because it did because now this one
is firing and if you'd like to see
evidence that both the Gateway event and
uh the stored script are firing we can
do this we can say both a logger for
this came from a Gateway script and uh
then underneath it this came from a
stored script and if we save it and
delete one of our
tags delete okay apply okay and hit save
all right so 80 drops off but more
importantly both of those just ran and
so if you look at the script you'll see
this came from a project stored script
fires this way came from a Gateway
script this came from project storage
script this came from a Gateway script
okay so you see them firing one after
another um because also every time you
save in a project your tag change uh
Gateway events are going to fire to
cover their butts because they don't
necessarily know if the tag changed
during that saving instance so just know
that as well if you're seeing weird
behavior and logs of like multiple
multiple instances odds are you're doing
contrl s a whole lot which I do fairly
fre frequently um but okay anyways uh
things we covered super speed we covered
how to use the script console for basic
code uh um compilation we covered how to
put put scripting directly on a tag with
value changed uh we even covered how to
generate and delete tags a little bit uh
using ignition scripting in Python which
is useful and then we covered Gateway
tag change events as well as storing
scripting so this is a lot to cram into
one tutorial I know this was pretty
dense and I know I moved pretty fast but
there was a lot to cover so I hope you
found it useful I hope you've been
enjoying the ignition tutorial series uh
if so please leave a like on the video
subscribe to the channel let me know in
the comments below what you'd like to
see next on the channel don't forget to
check out the uh sponsor Link in the
description of the video as well and if
you would like to consider becoming a
Super supporter of the channel my
patreon link is down there below uh I
appreciate the hack out of everyone who
liked and supports the Master Tech
YouTube in any way um so as always thank
you for watching we'll see you next time
thanks bye