As with Brexit in Britain, the outcome of a referendum in Hamburg came as a blow to the establishment. By the slimmest of majorities, voters defied the status quo, and the party that did the best job convincing the public it would honour their will collected the political rewards.

The dynamic unleashed by the 2013 plebiscite on buying the local utility grids back from private providers helps to explain why the city state is expected to buck national trends at this Sunday’s elections.

Polls predict the ruling centre-left Social Democrats (SPD), who are in decline across Germany, will hang on to their Hamburg stronghold with relative ease. Their junior coalition partner, the Green party, is expected to increase its share of the vote, leading to a strengthened mandate for the current power-sharing deal.

The CDU of Angela Merkel is likely to drop into third place while the far-right Alternative für Deutschland may even struggle to make it above the 5% threshold for entry into the Hamburg parliament.

Yet a centre-left success in the city state is more likely to be the result of local factors than national trends.

In September 2013, 51% of the electorate voted to “re-municipalise” the local electricity and district heating networks, as proposed by grassroots initiative “Our Hamburg Our Grid”. And while the energy debate has only played a minor role in this year’s election campaign, it has increased the profile of the two governing parties.

For one, Hamburg has managed to get de-privatisation done. In Berlin, by contrast, the senate announced its intention to buy back the district heating grid but has so far failed to drive the project through the courts. Hamburg sealed a €650m (£545m) deal last September.

For Hamburg’s SPD – which campaigned against re-municipalisation, and is widely seen as more pro-business than elsewhere in the country – the buy-back has helped show it can also apply its famed pragmatism to more traditionally leftwing projects.

The local party has been quicker to discover its green heart than other SPD branches in the country. And on Thursday, Hamburg’s SPD mayor, Peter Tschentscher, surprised his Green coalition partners by proposing to shut one coal unit at the city’s Moorburg power plant and convert the second to gas, in addition building a 100MW electrolyser to produce green hydrogen at the site.

“If we hadn’t listened to voters, we would have been punished,” said Matthias Ederhof, an SPD candidate on the regional party list. “We are on the up because voters see us as competent.”

The re-municipalised electricity grid, for which Hamburg paid the Swedish energy group Vattenfall €610m, has been making the city senate a healthy profit since 2017.

The Greens are finding that having utility grids in public hands allows them to propose initiatives that would otherwise have sounded like pipe dreams. Hamburg, a city whose wealth has historically grown around its inland port, has in recent years turned into a busy laboratory for renewable energy schemes.

Hamburg is now the first state in Germany to write a legally binding date into its constitution, the year 2030, not just for halving its carbon emissions from 1990 levels, but also for phasing out coal power for district heating altogether. The 250,000 households in the city which use district heating are still powered by coal stations in Wedel and Tiefstack.

In the future, the Greens propose, Hamburg homes will be heated entirely from renewable sources such as waste incineration, biomass or solar power. There is a proposal to build a large heat storage system underneath the harbour, capturing excess heat produced in the summer for use during the winter by pumping hot salt water hundreds of meters deep into the soil.

Even an old air raid bunker in the Wilhelmsburg district has been transformed into a so-called “energy bunker”, with a large heat reservoir. The city has the highest number of charging points for electric cars in Germany, more than Berlin or Munich.

If Hamburg’s utility grid was still owned by Vattenfall, such experiments would have been unthinkable, said Christian Maass, a former state secretary for the environment and urban planning.

“Private monopolies become a problem especially when the state doesn’t have the power and capacity to regulate them. In Hamburg, it therefore made more sense for the senate to hold the monopoly, because politicians can be held to account much more effectively than private companies,” said Maass, who now works as consultant on renewable energy.

“Expectations on state-owned energy or heating grids are much more long-term and less orientated around maximising profit. If you want to shape the future of a city, that’s an immeasurable advantage.”