A thermal management system for controlling the temperature in a cabin and an energy storage system of an electric vehicle including a vehicle component and a cooling loop including a thermal fluid for cooling the vehicle component, the system comprising: - one heat exchanger arranged to heat the energy storage system, - one heater arranged to heat the cabin and to provide heat to the heat exchanger, and - a control unit configured to: ∘ receive data associated with a measured temperature in the cabin, ∘ receive data associated with a measured temperature in the energy storage system, ∘ determine if any of the cabin or the energy storage system needs to be heated, based on the received data, and a first valve disposed in the cooling loop and having an inlet arranged to receive the thermal fluid that has been used for cooling the vehicle component, and an outlet in fluid communication with the heater, characterized in that the system comprises: - a temperature sensor arranged to measure the temperature of the thermal fluid entering the inlet of the first valve, wherein the temperature sensor is arranged in the first valve or in a passage before the thermal fluid enters the first valve, - a second valve having an inlet arranged to receive the thermal fluid from the heater, a first outlet in fluid communication with the cabin, and a second outlet in fluid communication with the heat exchanger, and the control unit is configured to: ∘ receive the measured temperature of the thermal fluid from the temperature sensor, ∘ determine if there is excess heat in the thermal fluid entering the inlet of the first valve based on the measured temperature from the temperature sensor, ∘ control the outlet of the first valve so that the thermal fluid is provided to the heater when there is excess heat in the thermal fluid and any of the energy storage system and the cabin needs to be heated, and ∘ control the second valve so that the thermal fluid from the heater is distributed to the cabin and/or to the heat exchanger based on the need of heating of the cabin and the energy storage system. - one heat exchanger arranged to heat the energy storage system, - one heater arranged to heat the cabin and to provide heat to the heat exchanger, and - a control unit configured to: ∘ receive data associated with a measured temperature in the cabin, ∘ receive data associated with a measured temperature in the energy storage system, ∘ determine if any of the cabin or the energy storage system needs to be heated, based on the received data, and ∘ receive data associated with a measured temperature in the cabin, ∘ receive data associated with a measured temperature in the energy storage system, ∘ determine if any of the cabin or the energy storage system needs to be heated, based on the received data, and - a temperature sensor arranged to measure the temperature of the thermal fluid entering the inlet of the first valve, wherein the temperature sensor is arranged in the first valve or in a passage before the thermal fluid enters the first valve, - a second valve having an inlet arranged to receive the thermal fluid from the heater, a first outlet in fluid communication with the cabin, and a second outlet in fluid communication with the heat exchanger, and the control unit is configured to: ∘ receive the measured temperature of the thermal fluid from the temperature sensor, ∘ determine if there is excess heat in the thermal fluid entering the inlet of the first valve based on the measured temperature from the temperature sensor, ∘ control the outlet of the first valve so that the thermal fluid is provided to the heater when there is excess heat in the thermal fluid and any of the energy storage system and the cabin needs to be heated, and ∘ control the second valve so that the thermal fluid from the heater is distributed to the cabin and/or to the heat exchanger based on the need of heating of the cabin and the energy storage system. ∘ receive the measured temperature of the thermal fluid from the temperature sensor, ∘ determine if there is excess heat in the thermal fluid entering the inlet of the first valve based on the measured temperature from the temperature sensor, ∘ control the outlet of the first valve so that the thermal fluid is provided to the heater when there is excess heat in the thermal fluid and any of the energy storage system and the cabin needs to be heated, and ∘ control the second valve so that the thermal fluid from the heater is distributed to the cabin and/or to the heat exchanger based on the need of heating of the cabin and the energy storage system.