A system for managing power flow or energy in a nano- or microgrid, comprising at least one renewable energy source and at least one local load (221, 222), and between the nano- or microgrid and a wide area synchronous grid comprising at least one phase, the system comprising: - an input module connectable to said at least one phase and configured to measure at least one electrical input parameter of the at least one phase and to provide at least one electrical input parameter data; - at least one primary switching means (S1, S2), configured to connect or disconnect the nano- or microgrid to or from one or more of said at least one phase, resulting in an off-grid or on-grid mode of operation of the nano- or microgrid; and - at least one secondary switching means (S3, S4) configured to connect or disconnect one or more of the at least one renewable energy source to or from one or more of said at least one local load (221, 222), characterized in that the system further comprises: - a control unit having inputs for: - receiving the measured at least one electrical input parameter from the input module; - receiving local power generation data from a unit within the nano- or microgrid which measures the power actually generated by means of said at least one renewable energy source; and - receiving external power generation data from an external provider, said external power generation data being representative of the power that theoretically should be generated by means of said at least one renewable energy source based on external factors, and wherein the control unit is provided for: - calculating a power difference value based on the local power generation data and the external power generation data; - determining a rate of change of the power difference value; - operating a decision making process based on the determined rate of change of the power difference value and on the measured at least one electrical input parameter; and - operating one or more of the at least one primary switching means (S1, S2) and the at least one secondary switching means (S3, S4) based on a result of the decision making process. - an input module connectable to said at least one phase and configured to measure at least one electrical input parameter of the at least one phase and to provide at least one electrical input parameter data; - at least one primary switching means (S1, S2), configured to connect or disconnect the nano- or microgrid to or from one or more of said at least one phase, resulting in an off-grid or on-grid mode of operation of the nano- or microgrid; and - at least one secondary switching means (S3, S4) configured to connect or disconnect one or more of the at least one renewable energy source to or from one or more of said at least one local load (221, 222), - a control unit having inputs for: - receiving the measured at least one electrical input parameter from the input module; - receiving local power generation data from a unit within the nano- or microgrid which measures the power actually generated by means of said at least one renewable energy source; and - receiving external power generation data from an external provider, said external power generation data being representative of the power that theoretically should be generated by means of said at least one renewable energy source based on external factors, and - receiving the measured at least one electrical input parameter from the input module; - receiving local power generation data from a unit within the nano- or microgrid which measures the power actually generated by means of said at least one renewable energy source; and - receiving external power generation data from an external provider, said external power generation data being representative of the power that theoretically should be generated by means of said at least one renewable energy source based on external factors, and - calculating a power difference value based on the local power generation data and the external power generation data; - determining a rate of change of the power difference value; - operating a decision making process based on the determined rate of change of the power difference value and on the measured at least one electrical input parameter; and - operating one or more of the at least one primary switching means (S1, S2) and the at least one secondary switching means (S3, S4) based on a result of the decision making process.