This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

multiple identical scripts
#1
I want to ask the opinion of the expert minds in source code ...

Sometimes we find ourselves in an installation that requires a large number of scripts and they are all the same, with the only difference that we only have to change the last numbers of the group address.

For this case it has occurred to me to do it like this, I have tried it and it works. I share it with you!


Code:
--ID Zone
i = 6

--Setpoint temperature control object
TC = '2/2/'.. i

--Fan speed control object AUTO MODE
FAN = '2/3/20'.. i

--COOL / HEAT mode thermostat status
Modo = '2/5/10'.. i

--ECO MODE control object
ECO = '3/5/'.. i

--Temperature object ECO MODE-COLD
TEC = '40/0/1'

--Temperature object ECO MODE-HEAT
TEH = '40/0/2'
Reply
#2
It is usually  project specific but I would recommend to play alias game. When you ignore the groups and focus on objects names then you can write universal scripts for many projects. You only need to stick to same object naming convention which is usually the case. 
https://forum.logicmachine.net/showthrea...0#pid20520
------------------------------
Ctrl+F5
Reply
#3
Another option is to wrap your code into a function, add it to common functions. Then call this function from scripts passing the required parameters. Optionally you can attach your script to a tag and calculate parameters based on calling group address.
Reply
#4
Hello, is it possible to add a number to a word?

an example that does not work so that you understand me...



Code:
aaa1 = '1/1/1'
bbb1 = grp.getvalue('1/1/2')

i = 1
if value then
  grp.write(aaa.. i, bbb.. i)
end
Reply
#5
.. is concatenation so '1/1/1' .. 1 is '1/1/11' not '1/1/2'.

To increase a group address by a number it needs to be converted to a numeric value first. grp.* functions accept addresses in numeric format so no need to convert back to a group address string.
Code:
addr = buslib.encodega('1/1/1') + 1

Your example does not work also because variables are named differently: aaa1 / aaa, bbb1 / bbb.
Reply
#6
I think they have not understood me. I know the script doesn't work, I want to use the same condition several times within the same script but just changing a number.

how can i make it work??

The example would be something like this.



Code:
aaa1 = '1/1/1'
aaa2 = '1/1/3'


bbb1 = grp.getvalue('1/1/2')
bbb2 = grp.getvalue('1/1/4')


value = event.getvalue()

i = 1
if value == 1 then
  grp.write(aaa.. i, bbb.. i)
end


i = 2
if value == 2 then
  grp.write(aaa.. i, bbb.. i)
end
Reply
#7
You can use tables for this. If event value is 1 then value of 1/1/2 will be written to 1/1/1, if event value is 2 then value of 1/1/4 will be written to 1/1/3. For all other event values nothing will happen.

Code:
dstaddrs = { '1/1/1', '1/1/3' }
valaddrs = { '1/1/2', '1/1/4' }

index = event.getvalue()
dstaddr = dstaddrs[ index ]

if dstaddr then
  value = grp.getvalue(valaddrs[ index ])
  grp.write(dstaddr, value)
end
Reply


Forum Jump: