What's wrong in my function

Hello,

my function works only in one direction. Either she only goes once or only once. depending on whether I switch on or off in the function. I can not find the mistake. in the debug is to see that from the function only one off comes, although the switch on outputs.

I think the problem is how you are parsing the input.

var payload = msg.payload;
if (payload.state == "ON")

There is no msg.payload.state as far as I can see.
I'm not sure what the switch is sending, but usually that is just msg.payload

Suggestion to help you:
add in line 2:
node.warn(msg.payload.state) and see what you get.
You may also want to then try chainging line 3 to:
if (msg.payload == "ON")

You can see clearly in the debug node the ON and OFF messages.
And they are in the msg.payload part of the message.

I can not really thank you. you saved my evening. I've been fiddling with it for so long. thank you so much for your help.

Could you please "tick" the post I made which solved it as the solution?