Logic Machine Forum
Script to calculate how many - 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: Script to calculate how many (/showthread.php?tid=3987)



Script to calculate how many - Fahd - 11.04.2022

Hi guys, 

Please, I need help with a script that should calculate how many thermostats are on a specific mode (Comfort,Economy,Away), I have about 120 thermostat and each thermostat have a 1byte unsigned integer DPT for the HVAC Mode (1 == Comfort, 2 == Away, 3 == Economy)
What I want to display is, 
Thermostats on Comfort Mode: 44
Thermostats on Away Mode: 22  
etc...
Also, is there any way to block the change temp in the LM5 if the Mode is not on Comfort?
To change temp the user should write a value between -3,+3  and its 2byte floating, you can see the picture attached 
   


RE: Script to calculate how many - admin - 11.04.2022

You need to tag all thermostat mode objects with therm-mode tag then the total count per mode can be calculated like this (either event script attached to this tag or a resident script with at least 5 second sleep time):
Code:
counters = {}

objects = grp.tag('therm-mode')

for _, obj in ipairs(objects) do
  value = obj.value
  counters[ value ] = (counters[ value ] or 0) + 1
end

grp.checkupdate('32/1/1', counters[ 1 ] or 0) -- comfort
grp.checkupdate('32/1/2', counters[ 2 ] or 0) -- away
grp.checkupdate('32/1/3', counters[ 3 ] or 0) -- economy

There are several options for blocking writes. One option is to use Custom JavaScript to block certain UI elements based on an object value. Another option is to add additional objects with event scripts which pass the value to the control object only when the mode object has a specific value.


RE: Script to calculate how many - Fahd - 11.04.2022

(11.04.2022, 12:27)admin Wrote: You need to tag all thermostat mode objects with therm-mode tag then the total count per mode can be calculated like this (either event script attached to this tag or a resident script with at least 5 second sleep time):
Code:
counters = {}

objects = grp.tag('therm-mode')

for _, obj in ipairs(objects) do
  value = obj.value
  counters[ value ] = (counters[ value ] or 0) + 1
end

grp.checkupdate('32/1/1', counters[ 1 ] or 0) -- comfort
grp.checkupdate('32/1/2', counters[ 2 ] or 0) -- away
grp.checkupdate('32/1/3', counters[ 3 ] or 0) -- economy

There are several options for blocking writes. One option is to use Custom JavaScript to block certain UI elements based on an object value. Another option is to add additional objects with event scripts which pass the value to the control object only when the mode object has a specific value.
Thank you so much for the script, 

Could you give one solution for blocking the writing?


RE: Script to calculate how many - admin - 12.04.2022

Here's an example of blocking via a script and an additional virtual object. Single script can be used for multiple objects when mapped to a tag.

All objects must follow a fixed addressing scheme where the last group address part matches for all three objects.
In this example thermostat mode is 32/1/X, output is 33/1/X, this can be changed as needed.

Code:
addr = event.dst:split('/')[3]
mode = grp.getvalue('32/1/' .. addr)

-- write input value to output only when mode is comfort (1)
if mode == 1 then
  value = event.getvalue()  
  grp.checkwrite('33/1/' .. addr, value)
end



RE: Script to calculate how many - Fahd - 12.04.2022

Thank you admin, but it didn't work for me,
All of my objects is already following a fixed address,

14/4/x is for HVAC Mode, 14/3/X is for Shift set Point

That's what I did

- I added a tag named "shift" for 14/3/12 and created an event-based script with the tag shift

addr = event.dstConfusedplit('/')[3]
mode = grp.getvalue('14/4/' .. addr)

-- write input value to output only when the mode is comfort (1)
if mode == 1 then
value = event.getvalue()
grp.checkwrite('14/3/' .. addr, value)
end
then went to the visulasation and changed the mode to away, and tried to change the temp and it changed

Have I done something wrong?


RE: Script to calculate how many - admin - 12.04.2022

You need additional virtual objects for this script to work as you cannot block the write that has already happened. For example, 32/1/X that will be used in the visualization and will have the event script attached to it. Depending on the HVAC mode the value will be passed to 14/3/X or not.


RE: Script to calculate how many - Fahd - 12.04.2022

(12.04.2022, 11:37)admin Wrote: You need additional virtual objects for this script to work as you cannot block the write that has already happened. For example, 32/1/X that will be used in the visualization and will have the event script attached to it. Depending on the HVAC mode the value will be passed to 14/3/X or not.

Thank you so much  Big Grin