The idea of grid friendly charging is to use electricity from the grid to charge batteries when electricity is available in surplus and cheap. The goal is twofold: to avoid putting additional load on the electricity grid and to reduce the cost to the consumer. To achieve this, a smart meter and a tariff with variable electricity prices has to be in place. In Day Ahead tariff (DA), prices are announced in advance for the next day, and this information can be used to select the cheapest times to charge the battery by the required amount. The optimization method is very simple, and it only has to be run once per day. However, the balance of supply and demand is not fully known in advance. Therefore Real Time Pricing (RTP) tariff supplies electricity at spot market rate depending on the current balance. This makes the charging process less predictable because it adds a stochastic element, but it does offer the potential of higher savings if future prices can be predicted with a reasonable degree of accuracy. This paper proposes an optimal controller based on a stochastic dynamic program (SDP), which predicts future price changes from available data. The controller takes into account price variability via a simple grid model that allows of unexpected price rises and a gradual return to a normal grid price. The DP algorithm has two variables, the state of charge (SoC) and the current electricity cost. It traces the expected total cost based on the stochastic model and makes a decision 'to charge or not' to minimize the expected (average) total cost. The results show that in case of a positive probability of price rises, the time to charge is chosen slightly before the lowest expected cost during the night. This is a rational solution, because waiting longer does increase the risk of an unexpected price spike. In the trivial case of a zero probability of unexpected price rises, the solution converges to the one found by the previous deterministic optimization algorithm.