A method for controlling a hydraulic system for a working machine, the system comprising: a first electric machine connected to a first hydraulic machine, the first hydraulic machine comprising an input side and an output side; a second electric machine connected to a second hydraulic machine, the second hydraulic machine comprising a high-pressure side and a low-pressure side, the high-pressure side of the second hydraulic machine being connected to the input side of the first hydraulic machine; at least one hydraulic consumer hydraulically coupled to the output side of the first hydraulic machine via a supply line and configured to be powered by the first hydraulic machine; a first return line hydraulically coupling the hydraulic consumer to the input side of the first hydraulic machine and to the high-pressure side of the second hydraulic machine; wherein the at least one hydraulic consumer comprises a first hydraulic cylinder connected to the first return line and wherein the hydraulic system further comprises second and third hydraulic cylinders (202, 204) connected to a return valve block configured and arranged to control the return flow direction from the second and third hydraulic cylinders (202, 204) to the second hydraulic machine, the return valve block being coupled to a second return line connected to the input side of the first hydraulic machine and to the high-pressure side of the second hydraulic machine, the return valve block being further coupled to a third return line connected to the low-pressure side of the second hydraulic machine wherein the method comprises: detecting a return flow from the hydraulic consumer through the first return line; controlling the second hydraulic machine to maintain a pressure in the first return line at a pressure level higher than a predetermined minimum pressure level; and controlling the return valve block based on operating properties of the first, second and/or third hydraulic cylinder such that energy is recovered by the first or second hydraulic machine. a first electric machine connected to a first hydraulic machine, the first hydraulic machine comprising an input side and an output side; a second electric machine connected to a second hydraulic machine, the second hydraulic machine comprising a high-pressure side and a low-pressure side, the high-pressure side of the second hydraulic machine being connected to the input side of the first hydraulic machine; at least one hydraulic consumer hydraulically coupled to the output side of the first hydraulic machine via a supply line and configured to be powered by the first hydraulic machine; a first return line hydraulically coupling the hydraulic consumer to the input side of the first hydraulic machine and to the high-pressure side of the second hydraulic machine; wherein the at least one hydraulic consumer comprises a first hydraulic cylinder connected to the first return line and wherein the hydraulic system further comprises second and third hydraulic cylinders (202, 204) connected to a return valve block configured and arranged to control the return flow direction from the second and third hydraulic cylinders (202, 204) to the second hydraulic machine, the return valve block being coupled to a second return line connected to the input side of the first hydraulic machine and to the high-pressure side of the second hydraulic machine, the return valve block being further coupled to a third return line connected to the low-pressure side of the second hydraulic machine wherein the method comprises: detecting a return flow from the hydraulic consumer through the first return line; controlling the second hydraulic machine to maintain a pressure in the first return line at a pressure level higher than a predetermined minimum pressure level; and controlling the return valve block based on operating properties of the first, second and/or third hydraulic cylinder such that energy is recovered by the first or second hydraulic machine. detecting a return flow from the hydraulic consumer through the first return line; controlling the second hydraulic machine to maintain a pressure in the first return line at a pressure level higher than a predetermined minimum pressure level; and controlling the return valve block based on operating properties of the first, second and/or third hydraulic cylinder such that energy is recovered by the first or second hydraulic machine.