It’s not correct, but it lead me to find what worked.
Don’t ask me why but it has to be …state == “1” or with three equal signs.
I also believe you made a typo with return return rgb on state off?
The working code in configurator.yaml is
switch.sonoff_1000000000:
friendly_name: Kaffekokaren
templates:
icon: mdi:coffee-maker
icon_color: if (state === 'off') return ''; # this returns the icon in an unchanged state
if (entities['counter.counter'].state == "0") return 'rgb(255, 0, 0)';
if (entities['counter.counter'].state == "1") return 'rgb(235, 20, 0)';
if (entities['counter.counter'].state == "2") return 'rgb(200, 50, 0)';
if (entities['counter.counter'].state == "3") return 'rgb(150, 100, 0)';
if (entities['counter.counter'].state == "4") return 'rgb(100, 150, 0)';
Now I just have to fine tune the color changes.