Help to optimize a flow for data synchronization

I made a flow, which reads a data from 4 victron energy devices. From one device I'm making a 15sec data average and on the end I'm waiting when all data are received. But for me it look quite complicate. Please can you check my code and tell me what can I improve ?

[{"id":"062f6be4b36ff190","type":"victron-input-solarcharger","z":"59fb2f9263e14661","service":"com.victronenergy.solarcharger.socketcan_can0_vi0_uc104492","path":"/Yield/Power","serviceObj":{"service":"com.victronenergy.solarcharger.socketcan_can0_vi0_uc104492","name":"MPPT 450/100 HQ2044EQ1UC","paths":[{"path":"/Alarms/HighVoltage","type":"enum","name":"High batt. voltage alarm","enum":{"0":"No alarm","1":"Alarm"}},{"path":"/Alarms/LowVoltage","type":"enum","name":"Low batt. voltage alarm","enum":{"0":"No alarm","1":"Alarm"}},{"path":"/Dc/0/Current","type":"float","name":"Current (A)"},{"path":"/Dc/0/Temperature","type":"float","name":"Battery temperature (C)"},{"path":"/Dc/0/Voltage","type":"float","name":"Voltage (V)"},{"path":"/ErrorCode","type":"enum","name":"Error code","enum":{"0":"No error","1":"#1 - Battery temperature too high","2":"#2 - Battery voltage too high","3":"#3 - Battery temperature sensor miswired (+)","4":"#4 - Battery temperature sensor miswired (-)","5":"#5 - Battery temperature sensor disconnected","6":"#6 - Battery voltage sense miswired (+)","7":"#7 - Battery voltage sense miswired (-)","8":"#8 - Battery voltage sense disconnected","9":"#9 - Battery voltage wire losses too high","10":"#10 - Battery voltage too low","11":"#11 - Battery ripple voltage on terminals too high","12":"#12 - Battery low state of charge","13":"#13 - Battery mid-point voltage issue","14":"#14 - Battery temperature too low","17":"#17 - Charger temperature too high","18":"#18 - Charger over-current","19":"#19 - Charger current polarity reversed","20":"#20 - Max Bulk-time exceeded","21":"#21 - Charger current sensor issue","22":"#22 - Temperature sensor miswired","23":"#23 - Charger temperature sensor disconnected","24":"#24 - Charger internal fan not detected","25":"#25 - Charger internal fan over-current","26":"#26 - Charger terminal overheated","27":"#27 - Charger short circuit","28":"#28 - Charger issue with power stage","29":"#29 - Over-charge protection","31":"#31 - Input voltage out of range","32":"#32 - Input voltage too low","33":"#33 - Input voltage too high","34":"#34 - PV over current","35":"#35 - Input excessive power","36":"#36 - Input polarity issue","37":"#37 - Input voltage absent (mains removed, fuse blown?)","38":"#38 - Input shutdown due to battery over-voltage","39":"#39 - Input shutdown due to battery over-voltage","40":"#40 - Internal failure (PV Input failed to shutdown)","41":"#41 - Inverter shutdown (panel isolation resistance too low)","42":"#42 - Inverter shutdown (ground current too high: >30mA)","43":"#43 - Inverter shutdown (voltage over ground relay too high)","50":"#50 - Inverter overload (iit protection)","51":"#51 - Inverter temperature too high","52":"#52 - Inverter excessive current","53":"#53 - Inverter dc level (internal dc rail voltage)","54":"#54 - Inverter ac level (output voltage not ok)","55":"#55 - Inverter dc fail (dc on output)","56":"#56 - Inverter ac fail (shape wrong)*/","57":"#57 - Inverter ac on output (inverter only)","58":"#58 - Inverter bridge fault (hardware signal)","59":"#59 - ACIN1 relay test fault","60":"#60 - ACIN2 relay test fault","65":"#65 - Device disappeared during parallel operation (broken cable?)","66":"#66 - Incompatible device encountered for parallel operation (e.g. old firmware/different settings)","67":"#67 - No BMS","68":"#68 - Network misconfigured","113":"#113 - Non-volatile storage write error","114":"#114 - CPU temperature to high","115":"#115 - CAN/SCI communication lost (when critical)","116":"#116 - Calibration data lost","117":"#117 - Incompatible firmware encountered","118":"#118 - Incompatible hardware encountered","119":"#119 - Settings data lost","120":"#120 - Reference voltage failure","121":"#121 - Tester fail","122":"#122 - Non-volatile history data invalid/corrupted","200":"#200 - Internal error","201":"#201 - Internal error","203":"#203 - Internal error","205":"#205 - Internal error","212":"#212 - Internal error","215":"#215 - Internal error"}},{"path":"/History/Daily/0/MaxPower","type":"float","name":"Maximum charge power today (W)"},{"path":"/History/Daily/0/Pv/0/MaxPower","type":"float","name":"Maximum charge power today for tracker 0 (W)"},{"path":"/History/Daily/0/Pv/0/Yield","type":"float","name":"Yield today for tracker 0 (kWh)"},{"path":"/History/Daily/0/Pv/1/MaxPower","type":"float","name":"Maximum charge power today for tracker 1 (W)"},{"path":"/History/Daily/0/Pv/1/Yield","type":"float","name":"Yield today for tracker 1 (kWh)"},{"path":"/History/Daily/0/Yield","type":"float","name":"Yield today (kWh)"},{"path":"/History/Daily/1/MaxPower","type":"float","name":"Maximum charge power yesterday (W)"},{"path":"/History/Daily/1/Pv/0/MaxPower","type":"float","name":"Maximum charge power yesterday tracker 0 (W)"},{"path":"/History/Daily/1/Pv/0/Yield","type":"float","name":"Yield yesterday for tracker 0 (kWh)"},{"path":"/History/Daily/1/Pv/1/MaxPower","type":"float","name":"Maximum charge power yesterday tracker 1 (W)"},{"path":"/History/Daily/1/Pv/1/Yield","type":"float","name":"Yield yesterday for tracker 1 (kWh)"},{"path":"/History/Daily/1/Yield","type":"float","name":"Yield yesterday (kWh)"},{"path":"/Mode","type":"enum","name":"Charger on/off","enum":{"1":"On","4":"Off"}},{"path":"/MppOperationMode","type":"enum","name":"MPP operation mode","enum":{"0":"Off","1":"Voltage or current limited","2":"MPPT Tracker active","255":"Not available"}},{"path":"/Pv/0/P","type":"float","name":"Tracker 1 power"},{"path":"/Pv/0/V","type":"float","name":"Tracker 1 voltage"},{"path":"/Pv/1/P","type":"float","name":"Tracker 2 power"},{"path":"/Pv/1/V","type":"float","name":"Tracker 2 voltage"},{"path":"/Relay/0/State","type":"enum","name":"Relay on the charger","enum":{"0":"Open","1":"Closed"}},{"path":"/State","type":"enum","name":"Charge state","enum":{"0":"Off","2":"Fault","3":"Bulk","4":"Absorption","5":"Float","6":"Storage","7":"Equalize","245":"Off","247":"Equalize","252":"Ext. Control"}},{"path":"/Yield/Power","type":"float","name":"PV Power (W)"},{"path":"/Yield/System","type":"string","name":"Yield since last update (kWh)"},{"path":"/Yield/User","type":"string","name":"Yield since reset (kWh)"}]},"pathObj":{"path":"/Yield/Power","type":"float","name":"PV Power (W)"},"initial":"","name":"","x":600,"y":820,"wires":[["c14573617849922c"]]},{"id":"b5dc593098bc3ecc","type":"victron-input-battery","z":"59fb2f9263e14661","service":"com.victronenergy.battery.socketcan_can1","path":"/Soc","serviceObj":{"service":"com.victronenergy.battery.socketcan_can1","name":"Pylontech battery","paths":[{"path":"/Alarms/CellImbalance","type":"enum","name":"Cell Imbalance alarm","enum":{"0":"No alarm","1":"Warning","2":"Alarm"}},{"path":"/Alarms/HighChargeCurrent","type":"enum","name":"High charge current alarm","enum":{"0":"No alarm","1":"Warning","2":"Alarm"}},{"path":"/Alarms/HighChargeTemperature","type":"enum","name":"High charge temperature alarm","enum":{"0":"No alarm","1":"Warning","2":"Alarm"}},{"path":"/Alarms/HighDischargeCurrent","type":"enum","name":"High discharge current alarm","enum":{"0":"No alarm","1":"Warning","2":"Alarm"}},{"path":"/Alarms/HighTemperature","type":"enum","name":"High battery temperature alarm","enum":{"0":"No alarm","2":"Alarm"}},{"path":"/Alarms/HighVoltage","type":"enum","name":"High voltage alarm","enum":{"0":"No alarm","2":"Alarm"}},{"path":"/Alarms/InternalFailure","type":"enum","name":"Internal error alarm","enum":{"0":"No alarm","1":"Warning","2":"Alarm"}},{"path":"/Alarms/LowChargeTemperature","type":"enum","name":"Low charge temperature alarm","enum":{"0":"No alarm","1":"Warning","2":"Alarm"}},{"path":"/Alarms/LowTemperature","type":"enum","name":"Low battery temperature alarm","enum":{"0":"No alarm","2":"Alarm"}},{"path":"/Alarms/LowVoltage","type":"enum","name":"Low voltage alarm","enum":{"0":"No alarm","2":"Alarm"}},{"path":"/Capacity","type":"float","name":"Capacity (Ah)"},{"path":"/Dc/0/Current","type":"float","name":"Current (A)"},{"path":"/Dc/0/Temperature","type":"float","name":"Battery temperature (C)"},{"path":"/Dc/0/Voltage","type":"float","name":"Voltage (V)"},{"path":"/History/ChargedEnergy","type":"float","name":"Charged Energy (kWh)"},{"path":"/History/DischargedEnergy","type":"float","name":"Discharged Energy (kWh)"},{"path":"/Info/BatteryLowVoltage","type":"float","name":"Min discharge voltage (V DC)"},{"path":"/Info/MaxChargeCurrent","type":"float","name":"CCL - Charge Current Limit (A)"},{"path":"/Info/MaxChargeVoltage","type":"float","name":"CVL - Charge Voltage Limit (V)"},{"path":"/Info/MaxDischargeCurrent","type":"float","name":"DCL - Discharge Current Limit (A)"},{"path":"/Soc","type":"float","name":"State of charge (%)"},{"path":"/Soh","type":"float","name":"State of health (%)"},{"path":"/System/MaxCellTemperature","type":"float","name":"Maximum cell temperature (Degrees celsius)"},{"path":"/System/MaxCellVoltage","type":"float","name":"System; maximum cell voltage (V DC)"},{"path":"/System/MinCellTemperature","type":"float","name":"Minimum cell temperature (Degrees celsius)"},{"path":"/System/MinCellVoltage","type":"float","name":"System; minimum cell voltage (V DC)"}]},"pathObj":{"path":"/Soc","type":"float","name":"State of charge (%)"},"initial":"","name":"","x":590,"y":880,"wires":[["c14573617849922c"]]},{"id":"313fed3360df0e3d","type":"victron-input-system","z":"59fb2f9263e14661","service":"com.victronenergy.system","path":"/Ac/ConsumptionOnOutput/L1/Power","serviceObj":{"service":"com.victronenergy.system","name":"com.victronenergy.system","paths":[{"path":"/Ac/ActiveIn/Source","type":"enum","name":"AC-Input","enum":{"0":"Not available","1":"Grid","2":"Generator","3":"Shore","240":"Inverting"}},{"path":"/Ac/Consumption/L1/Power","type":"float","name":"AC Consumption L1 (W)"},{"path":"/Ac/Consumption/L2/Power","type":"float","name":"AC Consumption L2 (W)"},{"path":"/Ac/Consumption/L3/Power","type":"float","name":"AC Consumption L3 (W)"},{"path":"/Ac/ConsumptionOnInput/NumberOfPhases","type":"integer","name":"AC Consumption on Input Number Of Phases"},{"path":"/Ac/ConsumptionOnOutput/L1/Power","type":"float","name":"AC Consumption on Output L1"},{"path":"/Ac/ConsumptionOnOutput/L2/Power","type":"float","name":"AC Consumption on Output L2"},{"path":"/Ac/ConsumptionOnOutput/L3/Power","type":"float","name":"AC Consumption on Output L3"},{"path":"/Ac/ConsumptionOnOutput/NumberOfPhases","type":"integer","name":"AC Consumption on Output Number Of Phases"},{"path":"/Ac/Genset/DeviceType","type":"float","name":"Genset Device Type","enum":{}},{"path":"/Ac/Genset/L1/Power","type":"float","name":"Genset L1 (W)"},{"path":"/Ac/Genset/L2/Power","type":"float","name":"Genset L2 (W)"},{"path":"/Ac/Genset/L3/Power","type":"float","name":"Genset L3 (W)"},{"path":"/Ac/Genset/NumberOfPhases","type":"integer","name":"Genset Number Of Phases"},{"path":"/Ac/Grid/DeviceType","type":"float","name":"Grid Device Type"},{"path":"/Ac/Grid/L1/Power","type":"float","name":"Grid L1 (W)"},{"path":"/Ac/Grid/L2/Power","type":"float","name":"Grid L2 (W)"},{"path":"/Ac/Grid/L3/Power","type":"float","name":"Grid L3 (W)"},{"path":"/Ac/Grid/NumberOfPhases","type":"integer","name":"Grid Number Of Phases"},{"path":"/Ac/PvOnGenset/L1/Power","type":"float","name":"PV Power AC-tied on Generator L1"},{"path":"/Ac/PvOnGenset/L2/Power","type":"float","name":"PV Power AC-tied on Generator L2"},{"path":"/Ac/PvOnGenset/L3/Power","type":"float","name":"PV Power AC-tied on Generator L3"},{"path":"/Ac/PvOnGenset/NumberOfPhases","type":"integer","name":"PV Power AC-tied on Generator Number Of Phases"},{"path":"/Ac/PvOnGrid/L1/Power","type":"float","name":"PV - AC-coupled on input L1 (W)"},{"path":"/Ac/PvOnGrid/L2/Power","type":"float","name":"PV - AC-coupled on input L2 (W)"},{"path":"/Ac/PvOnGrid/L3/Power","type":"float","name":"PV - AC-coupled on input L3 (W)"},{"path":"/Ac/PvOnGrid/NumberOfPhases","type":"integer","name":"PV - AC-coupled on input Number Of Phases"},{"path":"/Ac/PvOnOutput/L1/Power","type":"float","name":"PV - AC-coupled on output L1 (W)"},{"path":"/Ac/PvOnOutput/L2/Power","type":"float","name":"PV - AC-coupled on output L2 (W)"},{"path":"/Ac/PvOnOutput/L3/Power","type":"float","name":"PV - AC-coupled on output L3 (W)"},{"path":"/Ac/PvOnOutput/NumberOfPhases","type":"integer","name":"PV - AC-coupled on output Number Of Phases"},{"path":"/Buzzer/State","type":"float","name":"Buzzer State","enum":{"0":"Off","1":"On"}},{"path":"/Dc/Battery/ConsumedAmphours","type":"float","name":"Battery Consumed Amphours (Ah)"},{"path":"/Dc/Battery/Current","type":"float","name":"Battery current (A)"},{"path":"/Dc/Battery/Power","type":"float","name":"Battery Power (W)"},{"path":"/Dc/Battery/Soc","type":"float","name":"Battery State of Charge (%)"},{"path":"/Dc/Battery/State","type":"enum","name":"Battery state","enum":{"0":"idle","1":"charging","2":"discharging"}},{"path":"/Dc/Battery/Temperature","type":"float","name":"Battery temperature (C)"},{"path":"/Dc/Battery/TimeToGo","type":"float","name":"Battery Time to Go (h)"},{"path":"/Dc/Battery/Voltage","type":"float","name":"Battery voltage (V)"},{"path":"/Dc/Charger/Power","type":"integer","name":"AC-Chargers - power (W)"},{"path":"/Dc/Pv/Current","type":"float","name":"MPPTs - current (A)"},{"path":"/Dc/Pv/Power","type":"float","name":"MPPTs - power (W)"},{"path":"/Dc/System/Power","type":"float","name":"DC System (W)"},{"path":"/Dc/Vebus/Current","type":"float","name":"VE.Bus charge current (A)"},{"path":"/Dc/Vebus/Power","type":"float","name":"VE.Bus charge power (W)"},{"path":"/Serial","type":"string","name":"Serial (System)"},{"path":"/SystemType","type":"string","name":"System type"},{"path":"/Timers/TimeOff","type":"float","name":"Time off (s)"},{"path":"/Timers/TimeOnGenerator","type":"float","name":"Time generator (s)"},{"path":"/Timers/TimeOnGrid","type":"float","name":"Time grid (s)"},{"path":"/Timers/TimeOnInverter","type":"float","name":"Time inverting (s)"}]},"pathObj":{"path":"/Ac/ConsumptionOnOutput/L1/Power","type":"float","name":"AC Consumption on Output L1"},"initial":"","name":"","x":350,"y":1000,"wires":[["f50319dc808b1734"]]},{"id":"575acdb5021a5464","type":"victron-input-relay","z":"59fb2f9263e14661","service":"com.victronenergy.system","path":"/Relay/0/State","serviceObj":{"service":"com.victronenergy.system","name":"com.victronenergy.system","paths":[{"path":"/Relay/0/State","type":"enum","name":"Venus relay 1 state","enum":{"0":"Open","1":"Closed"}},{"path":"/Relay/1/State","type":"enum","name":"Venus relay 2 state","enum":{"0":"Open","1":"Closed"}}]},"pathObj":{"path":"/Relay/0/State","type":"enum","name":"Venus relay 1 state","enum":{"0":"Open","1":"Closed"}},"initial":"","name":"","x":560,"y":940,"wires":[["c14573617849922c"]]},{"id":"c14573617849922c","type":"function","z":"59fb2f9263e14661","name":"Wait for all tasks to finish","func":"context.data = context.data || new Object();\n\nswitch (msg.topic) {\n    case \"com.victronenergy.solarcharger.socketcan_can0_vi0_uc104492 - /Yield/Power\":\n        context.data.FVE = msg.payload;\n        msg = null;\n        break;\n    case \"com.victronenergy.system - /Ac/ConsumptionOnOutput/L1/Power\":\n        if (msg.payload.average != null)  {\n        context.data.LPower = msg.payload.average;\n        msg = null;\n        break;\n}else break;\n    case \"com.victronenergy.battery.socketcan_can1 - /Soc\":\n        context.data.SoC = msg.payload;\n        msg = null;\n        break;\n    case \"com.victronenergy.system - /Relay/0/State\":\n         context.data.Rele = msg.payload;\n        msg = null;\n        break;  \n    default:\n        msg = null;\n    \tbreak;\n}\nif(context.data.FVE != null && context.data.LPower != null && context.data.SoC != null && context.data.Rele != null) {\n\tmsg2 = new Object();\n    msg2 = context.data;\n    context.data=null;\n\treturn msg2;\n} else return msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1070,"y":940,"wires":[["4a24d3232b2b2c90"]]},{"id":"4a24d3232b2b2c90","type":"debug","z":"59fb2f9263e14661","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1270,"y":940,"wires":[]},{"id":"7cbfae2198e526ec","type":"function","z":"59fb2f9263e14661","name":"15sec avg","func":"var currentTime = new Date().getTime();\nif (!context.lastTime) {\n    context.lastTime = currentTime;\n    context.sum = msg.payload.value;\n    context.count = 1;\n}\nif (currentTime-context.lastTime > 15000) {\n    // calculate average for previous messages\n    msg.payload.average = context.sum/context.count;\n    // start tracking average again\n    context.sum = msg.payload.value;\n    context.count = 1;\n    context.lastTime = currentTime;\n} else {\n    context.sum += msg.payload.value;\n    context.count +=1;\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":830,"y":1000,"wires":[["c14573617849922c","32400dd5d3f63f91"]]},{"id":"32400dd5d3f63f91","type":"debug","z":"59fb2f9263e14661","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1070,"y":1000,"wires":[]},{"id":"f50319dc808b1734","type":"function","z":"59fb2f9263e14661","name":"value add","func":"msg.payload = {\n  value:msg.payload\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":660,"y":1000,"wires":[["7cbfae2198e526ec"]]}]

image
Thank you

I think you are trying to use context variables to save the values of earlier message payloads, but you are doing it wrong.

You access a context variable like this let mydata = context.get('temperature') | {};
And store it like this context.set('temperature', msg.payload.temperature);

But it is better to use a Join node to combine your solarcharger, battery and relay inputs into a single message.

I am completely bamboozled by your 15sec avg function:

var currentTime = new Date().getTime();
if (!context.lastTime) {
    context.lastTime = currentTime;
    context.sum = msg.payload.value;
    context.count = 1;
}
if (currentTime-context.lastTime > 15000) {
    // calculate average for previous messages
    msg.payload.average = context.sum/context.count;
    // start tracking average again
    context.sum = msg.payload.value;
    context.count = 1;
    context.lastTime = currentTime;
} else {
    context.sum += msg.payload.value;
    context.count +=1;
}
return msg;

Can you explain what you are trying to achieve ?

but before I combine all data I'm doing a 15sec average one of them and data are comming asynchronously , that's why I have waiting function for all data.

I'm not trying. It's working :slight_smile: I'm reading msg.payload.value and counting together till time is reached. When time is reached I divide by number of reads to get a average and store as msg.payload.average.

My apologies. I didn't mean "trying" to imply that it wasn't working.

it's ok, It was only to clarify :slight_smile:

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.