Logic Machine Forum
multiple identical scripts - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: multiple identical scripts (/showthread.php?tid=3180)



multiple identical scripts - davidchispas - 19.02.2021

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'



RE: multiple identical scripts - Daniel - 19.02.2021

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/showthread.php?tid=3169&pid=20520#pid20520


RE: multiple identical scripts - admin - 19.02.2021

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.


RE: multiple identical scripts - davidchispas - 09.11.2022

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



RE: multiple identical scripts - admin - 10.11.2022

.. 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.


RE: multiple identical scripts - davidchispas - 10.11.2022

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



RE: multiple identical scripts - admin - 10.11.2022

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