After a close fight, Barcelona’s radical mayor, Ada Colau, is expected to take office for a second term on Saturday, vindicating her often-criticised policies, which have included making sure all the city’s municipal buildings and services run on renewable energy.

On the other side of the country in Andalucía, José María González was re-elected as mayor of Cádiz last month having swept to power in 2015 on the same tide that brought Colau to office, with energy as a central issue in his campaign.

Cádiz and Barcelona have set themselves up as distributors, cutting out the middle man, and have begun installing solar panels on public buildings with the aim of becoming self-sufficient.

“Energy policy should be in the hands of the people,” González said. “In Cádiz we set up two permanent citizens’ energy forums and the people have been the driving force behind the improvements we have carried out. The people of Cádiz are the motor of energy transition.”

The city is an anomaly in that it has its own power company, Eléctrica de Cádiz, founded in 1929, in which the council holds a 55% stake. Since 2017 it has supplied all municipal needs and about 80% of households with energy from renewable sources.

About 40% of Andalucía’s power is produced by renewables, from windfarms and vast solar arrays such as those at San Roque, whose 67,000 panels rotate to follow the sun, and the innovative PS10 plant near Seville.

Cádiz is an ancient city but its elegant squares obscure the fact that it is the poorest province in Andalucía, Spain’s poorest region, and one of the most impoverished in the European Union. A quarter of the city’s population are unemployed.

Under González, €500,000 (£445,000) of the profits generated by Eléctrica de Cádiz is used to prevent “energy poverty” among the city’s most disadvantaged, whereas other companies simply cut off the supply to those who cannot afford to pay.

“The profits are invested in Cádiz, they don’t go to Qatar or a tax haven,” he said.

González believes the city’s sustainable policies were a factor in attracting the electric vehicle producer Torrot to establish its factory in Cádiz, with the creation of 200 jobs. In a city where shipbuilding dates back to Phoenician times, the shipyards have also diversified into building offshore wind platforms.

Cádiz embarked on an energy education programme to make people “energy literate”, to understand how energy is produced, to be responsible consumers and avoid being tricked into signing abusive contracts. More than 90% of citizens surveyed backed the move to renewables.

In wealthy Catalonia, Barcelona has also taken energy matters into its hands. When the power company that supplied the city failed to bid to renew its €33m contract last year, the council decided to set up Barcelona Energia (BE), to buy certified renewable energy direct from source.

“Our aims as a public company are to promote renewables and an ecological transition,” said Eloi Badia, responsible for the city’s energy policy. “We’re not looking to make a profit, and as a public service we can’t subsidise electricity because that would be unfair competition, but we can offer people a range of tariffs and, in the case of the most vulnerable, we can help directly and bypass welfare benefits and the like.”

“We don’t aim to be a producer but to represent all the small producers, to help with individual consumption,” Badia said. “We subsidise installation by up to 50% and offer discounts on VAT so that people can recoup the cost within three or four years. Since the law changed there’s been a tenfold increase in the number of projects. We don’t see other suppliers of renewable energy as competitors; on the contrary, we welcome them.”

However, as most people in the city live in apartment blocks, the issue is more complicated than installing solar panels on the roof of a house. Most blocks house a mix of owner-occupiers and tenants and Badia said there was as yet no legal framework for people living in apartments to share energy generated from communal solar panels.

But, as of January of this year, BE has started supplying electricity to private homes. Competition laws mean it can only supply 20% of the private market – about 20,000 households – but Badia said that as demand increases they could create subsidiary companies to meet it.

“We’re doing it in phases, learning as we go along,” he said. “We’re not in a hurry, we’ve only been going since last July and only supplying homes since January.”

Facebook Twitter Pinterest Wind turbines near Jerez de la Frontera, in Cádiz. Photograph: Stephen Burgen

Spain was on the way to becoming a world leader in renewables until the big power companies, alarmed that self-generation would cost them clients, persuaded the conservative government to introduce the so-called “sunshine tax” in 2013. Homeowners, far from being encouraged to install solar panels through the possibility of selling surplus energy to the grid, were instead treated as suppliers and taxed accordingly. As a result, Spain’s solar business went into a steep decline.

In contrast, Germany offers financial incentives to install solar panels, with the result that – with around half the sunshine hours – it generates 10 times more solar energy than Spain. The sunshine tax was scrapped after the socialists came to power in June 2018.

Though Spanish cities are making up for lost time, others are not doing so well and many EU member states will struggle to meet the 2020 renewable targets. A 2017 report commissioned by Friends of the Earth reveals that, while Reykjavik in Iceland and Fafe in Portugal use 100% renewables, major cities such as Amsterdam, Warsaw, Athens, London and Zurich are almost entirely dependent on fossil fuels. Wrocław in Poland and Gibraltar are the dirtiest in this respect, while three-quarters of the Paris power supply is nuclear.