[
{
"id": "58f4dcb609fc506a",
"type": "function",
"z": "966985d68ebb4990",
"g": "6f4cdc97add442ff",
"name": "kWh triggers",
"func": "var MQTT = global.get('MQTT', \"file\");\nif (MQTT.block_active == 0 || (MQTT.block_active == 1 && MQTT.block_mode == 2 && MQTT.allow_solar == 1)) {\n\n var F_SOLAR = global.get('F_SOLAR', \"file\");\n if (F_SOLAR === undefined) { return null; }\n\n if (F_SOLAR.state == 1) {\n var TOP4_Operating_Mode_State = global.get('TOP4_Operating_Mode_State', \"file\");\n var new_Operating_Mode_State; // just create a variable, empty.\n var old_Operating_Mode_State; // just create a variable, empty.\n var DHW_Current_SP = global.get('TOP9_DHW_Target_Temp', \"file\");\n var TOP0_Heatpump_State = global.get('TOP0_Heatpump_State', \"file\");\n var F_stored_next_operating_mode = global.get('F_stored_next_operating_mode', \"file\");\n let msg1 = {}; // pin 1 - output to DHW setpoint\n msg1.topic = 'Solar function'; // This topic is required to alow the managers.\n let msg2 = {}; // pin 2 - output to Operation Mode\n let msg3 = {}; // pin 3 - output to turn on power to heatpump.\n let msg99 = {}; // pin 4 - output to log\n msg99.topic = \"Function Solar\";\n // return [msg1,msg2,msg3,msg99]\n\n // Check if all required states are available.\n if (F_SOLAR.detection_method === undefined || F_SOLAR.threshold === undefined || F_SOLAR.fallback === undefined || F_SOLAR.dhw_sp_high === undefined || TOP4_Operating_Mode_State === undefined || F_SOLAR.power_override === undefined || TOP0_Heatpump_State === undefined) {\n msg99.payload = \"ERROR: Not all settings in 'Function Solar' are programmed in GUI yet.\";\n return [null, null, null, msg99]; // log output\n }\n if (global.get('Sterilization_active', \"file\") == 1) { return null; }\n \n //var TOP2_Force_DHW_State =global.get('TOP2_Force_DHW_State'); // Current DHW state (on / off). \n //if (global.get('TOP2_Force_DHW_State', \"file\") == 1) { return null; }\n\n \n // msg.topic = \"W\"; < > nul\n // msg.topic = 'GRID power production kWh';\n // msg.topic = 'GRID power consumption kWh';\n // msg.topic = '(+) Import (-) Export';\n\n //Threshold\n // kW\n // kWh_prod\n // kWh_result\n //\n // getting rid of all messages which are not related to the selected detection method.\n //\n if (F_SOLAR.detection_method == \"kW\" && msg.topic != 'W') { return null; }\n if (F_SOLAR.detection_method == \"kWh_prod\" && msg.topic != 'Export') { return null; }\n if (F_SOLAR.detection_method == \"kWh_result\" && msg.topic != 'Result') { return null; }\n if (msg.payload == 0) { return null; }\n\n // convert the W signal to kW so that the value (threshold) are similar.\n if (F_SOLAR.detection_method == \"kW\") { msg.payload = msg.payload / 1000; }\n\n\n\n\n // ACTIONS WHEN THE SOLAR FUNCTION IS TRIGGERED FOR START-UP.\n //////////////////////////////////////////////////////////////////////////////////////////////////////////////////\n // Note: (F_SOLAR.threshold > (msg.payload) >>> msg.payload is negative number if energy production is high.\n //////////////////////////////////////////////////////////////////////////////////////////////////////////////////\n //\n if (F_SOLAR.threshold > (msg.payload) && F_SOLAR.function_active_state == 0) {\n // Operating mode READ (0=Heat only, 1=Cool only, 2=Auto(Heat), 3=DHW only, 4=Heat+DHW, 5=Cool+DHW, 6=Auto(Heat)+DHW, 7=Auto(Cool), 8=Auto(Cool)+DHW)\n // Operating mode SET (0=Heat only, 1=Cool only, 2=Auto, 3=DHW only, 4=Heat+DHW, 5=Cool+DHW, 6=Auto+DHW\n\n new_Operating_Mode_State = 3; old_Operating_Mode_State = TOP4_Operating_Mode_State;\n if (TOP4_Operating_Mode_State == 7) { old_Operating_Mode_State = 2; } // correct OM in case of 7\n if (TOP4_Operating_Mode_State == 8) { old_Operating_Mode_State = 6; } // correct OM in case of 8\n\n /*\n if (TOP4_Operating_Mode_State == 0) { new_Operating_Mode_State = 4; old_Operating_Mode_State = 0 } // add DHW modus\n if (TOP4_Operating_Mode_State == 1) { new_Operating_Mode_State = 5; old_Operating_Mode_State = 1 } // add DHW modus\n if (TOP4_Operating_Mode_State == 2) { new_Operating_Mode_State = 6; old_Operating_Mode_State = 2 } // add DHW modus\n if (TOP4_Operating_Mode_State == 3) { new_Operating_Mode_State = 3; old_Operating_Mode_State = 3 } \n if (TOP4_Operating_Mode_State == 4) { new_Operating_Mode_State = 4; old_Operating_Mode_State = 4 } \n if (TOP4_Operating_Mode_State == 5) { new_Operating_Mode_State = 5; old_Operating_Mode_State = 5 } \n if (TOP4_Operating_Mode_State == 6) { new_Operating_Mode_State = 6; old_Operating_Mode_State = 6 } \n if (TOP4_Operating_Mode_State == 7) { new_Operating_Mode_State = 6; old_Operating_Mode_State = 2 } // add DHW modus\n if (TOP4_Operating_Mode_State == 8) { new_Operating_Mode_State = 6; old_Operating_Mode_State = 6 } // prevent undefined new_Operating_Mode_State\n */\n \n var tag; // declare variable for tag in log line.\n if (F_SOLAR.detection_method == \"kW\") { tag = 'kW'; }\n if (F_SOLAR.detection_method == \"kWh_prod\") { tag = \"Today's export (kWh)\"; }\n if (F_SOLAR.detection_method == \"kWh_result\") { tag = \"kWh todays result\"; }\n\n if (TOP0_Heatpump_State == 0 && F_SOLAR.power_override == 0) {\n msg99.payload = \"Solar threshold reached but no action fired! (Heat pump is off, 'Allow switch on heat pump' is disabled.)\";\n return ([null, null, null, msg99]);\n }\n\n if (F_stored_next_operating_mode === undefined) {\n global.set('F_stored_next_operating_mode', old_Operating_Mode_State, \"file\");\n msg99.payload = 'Stored next operating mode is set to ' + old_Operating_Mode_State ; node.send([null,null,null,msg99]); \n }\n\n // msg3///////////////////////////////////////////////////////////////////////////////////////////////////////\n // Turn on heatpump in case it is off\n if (TOP0_Heatpump_State == 0 && F_SOLAR.power_override == 1) {\n msg99.payload = \"Turn on Heatpump\";\n msg3.payload = 1; msg3.source = \"flow\"; // send 1 to turn on the heatpump\n if (global.get('MQTT_Source') === undefined) { global.set('MQTT_Source', \"Solar\"); }\n node.send([null, null, msg3, null])\n }\n\n\n\n // msg1///////////////////////////////////////////////////////////////////////////////////////////////////////\n // Set DHW setpoint to high\n if (DHW_Current_SP != F_SOLAR.dhw_sp_high) {\n msg1.payload = F_SOLAR.dhw_sp_high;\n msg99.payload = \"triggered threshold (\" + tag + \"). DHW SP to \" + F_SOLAR.dhw_sp_high;\n node.send([msg1, null, null, msg99]);\n }\n\n // msg2///////////////////////////////////////////////////////////////////////////////////////////////////////\n // Set Operation Mode\n if (new_Operating_Mode_State != old_Operating_Mode_State && old_Operating_Mode_State !== undefined) {\n msg2.payload = new_Operating_Mode_State;\n msg99.payload = \"Operation mode changed from \" + old_Operating_Mode_State + \" to \" + new_Operating_Mode_State;\n node.send([null, msg2, null, msg99]);\n }\n global.set('F_RTC.automation_active', 0, \"file\");\n if (F_SOLAR.function_active_state != 1) {\n global.set(\"F_SOLAR.function_active_state\", 1, \"file\");\n if (global.get('MQTT_Source') === undefined) { global.set('MQTT_Source', \"Solar\"); }\n return null;\n }\n }\n\n\n\n\n // ACTIONS WHEN THE SOLAR FUNCTION IS TRIGGERED FOR ENDING.\n //////////////////////////////////////////////////////////////////////////////////////////////////////////////////\n //\n if (msg.payload > 0 && F_SOLAR.detection_method == \"kW\" && DHW_Current_SP == F_SOLAR.dhw_sp_high && F_SOLAR.fallback == 1 && F_SOLAR.function_active_state == 1) {\n if (global.get('MQTT_Source') === undefined) { global.set('MQTT_Source', \"Solar\"); }\n msg1.payload = F_SOLAR.dhw_sp_low;\n msg99.payload = \"triggered threshold (\" + tag + \"). Above 0kWh threshold. DHW SP to \" + F_SOLAR.dhw_sp_low;\n global.set(\"F_SOLAR.function_active_state\", 0, \"file\");\n node.send([msg1, null, null, msg99]);\n return null;\n }\n\n //TOP9_DHW_Target_Temp\n // Operating mode (0=Heat only, 1=Cool only, 2=Auto(Heat), 3=DHW only, 4=Heat+DHW, 5=Cool+DHW, 6=Auto(Heat)+DHW, 7=Auto(Cool), 8=Auto(Cool)+DHW)\n\n var TOP10_DHW_Temp = global.get('TOP10_DHW_Temp');\n var TOP20_ThreeWay_Valve_State = global.get('TOP20_ThreeWay_Valve_State'); // threewayvalvue 1 = DHW, 0 = HEAT\n\n if (TOP10_DHW_Temp >= F_SOLAR.dhw_sp_high && F_SOLAR.function_active_state == 1) {\n /////////////////////////// \n\n msg99.payload = \"DHW temperature has reached SP high\";\n node.send([null, null, null, msg99])\n\n let myTimeout = setTimeout(f_rollbackaction, F_SOLAR.delay_after_completion * 60 * 1000); // <<<<<<<<<<<<<<<< DELAY \n node.status({ fill: \"red\", shape: \"ring\", text: \"\" });\n\n function f_rollbackaction() {\n var F_stored_next_operating_mode = global.get('F_stored_next_operating_mode', \"file\");\n\n var F_SOLAR = global.get('F_SOLAR', \"file\");\n var DHW_Current_SP = global.get('TOP9_DHW_Target_Temp', \"file\");\n //if (F_SOLAR.function_active_state == 2) { return null; }\n\n global.set(\"F_SOLAR.function_active_state\", 2, \"file\");\n\n msg99.payload = \"Cooldown period (\" + F_SOLAR.delay_after_completion + \" minutes) has ended. Solar function done for today\";\n node.send([null, null, null, msg99]);\n ///////////////////////////////////////////////////\n if (F_stored_next_operating_mode !== undefined) {\n TOP4_Operating_Mode_State = global.get('TOP4_Operating_Mode_State', \"file\")\n if (TOP4_Operating_Mode_State != F_stored_next_operating_mode) {\n msg2.payload = F_stored_next_operating_mode;\n if (global.get('MQTT_Source') === undefined) { global.set('MQTT_Source', \"Solar\"); }\n msg99.payload = \"Restoring previous OperatingMode: \" + msg2.payload;\n node.send([null, msg2, null, msg99]);\n }\n global.set('F_stored_next_operating_mode', undefined, \"file\");\n msg99.payload = 'Stored next operating mode is removed'; node.send([null, null, null, msg99]); \n }\n if (DHW_Current_SP != F_SOLAR.dhw_sp_low) {\n msg1.payload = F_SOLAR.dhw_sp_low;\n msg99.payload = \"Restoring DHW setpoint to: \" + F_SOLAR.dhw_sp_low + \"°C\";\n if (global.get('MQTT_Source') === undefined) { global.set('MQTT_Source', \"Solar\"); }\n node.send([msg1, null, null, msg99]);\n }\n clearTimeout(myTimeout);\n node.status({});\n } ////////////// end of f_rollbackaction\n }\n \n }\n}\n\n\n\n\n////////////////////////////////////////////////\n\n/*\nsetTimeout(function () {\n node.status({ fill: \"red\", shape: \"ring\", text: \"\" });\n\n global.set(\"F_SOLAR.function_active_state\", 2, \"file\");\n msg99.payload = \"End temperature detected\"\n node.send([null, null, null, msg99])\n if (F_stored_next_operating_mode !== undefined) {\n\n if (TOP4_Operating_Mode_State != F_stored_next_operating_mode) {\n msg1.payload = F_SOLAR.dhw_sp_low;\n msg2.payload = F_stored_next_operating_mode;\n if (global.get('MQTT_Source') === undefined) { global.set('MQTT_Source', \"Solar\"); }\n msg99.payload = \"DHW temperature reached DHW SP High. Restored previous OperatingMode: \" + msg2.payload + \" and restoring DHW setpoint to: \" + F_SOLAR.dhw_sp_low + \"°C\";\n node.send([msg1, msg2, null, msg99]);\n }\n\n global.set('F_stored_next_operating_mode', undefined, \"file\");\n return null;\n }\n\n else {\n msg99.payload = \"DHW temperature reached DHW SP High. Solar function ended for today\";\n return [null, null, null, msg99];\n }\n\n}, 30 * 60 * 1000); // revert OM 30 minutes later....\n \n }\n }\n \n }\n \n}\n\n*/",
"outputs": 4,
"timeout": "",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 350,
"y": 2920,
"wires": [
[
"78507acac200e031"
],
[
"0edffe2f6a90c7de"
],
[
"ee7e3fc1d241d09e"
],
[
"ba8f8e1c71858357"
]
],
"outputLabels": [
"DHW_SP",
"new_Operating_Mode_State",
"solar_turn_on_heatpump",
"log"
]
}
]