# What's wrong in my function

**URL:** https://discourse.nodered.org/t/whats-wrong-in-my-function/18624
**Category:** General
**Created:** [29 November 2019 21:33 UTC](https://discourse.nodered.org/t/whats-wrong-in-my-function/18624 "2019-11-29T21:33:53Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Mario](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@Mario](https://discourse.nodered.org/u/Mario)
#### Post date: [29 November 2019 21:33 UTC](https://discourse.nodered.org/t/whats-wrong-in-my-function/18624/1 "2019-11-29T21:33:53Z")

</div>

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.

 ![Flow](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/6/6dd7790153213a1ac6897f38194c1f6d4bdd619f.jpeg) ![Function](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/7/7555874864e461c36bd4fd7e6e9921a52be87670.jpeg)

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [29 November 2019 21:48 UTC](https://discourse.nodered.org/t/whats-wrong-in-my-function/18624/2 "2019-11-29T21:48:17Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

### Author: ![Mario](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@Mario](https://discourse.nodered.org/u/Mario)
#### Post date: [29 November 2019 22:08 UTC](https://discourse.nodered.org/t/whats-wrong-in-my-function/18624/3 "2019-11-29T22:08:52Z")

</div>

> [@Trying\_to\_learn](#):
>
> I'm not sure what the switch is sending, but usually that is just `msg.payload`

 ![Switch](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/d/dbb28f8da22cb970aa189d238272150a657ffe94.jpeg)

---

<div class="post-metadata">

### Author: ![Mario](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@Mario](https://discourse.nodered.org/u/Mario)
#### Post date: [29 November 2019 22:16 UTC](https://discourse.nodered.org/t/whats-wrong-in-my-function/18624/4 "2019-11-29T22:16:23Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [29 November 2019 23:35 UTC](https://discourse.nodered.org/t/whats-wrong-in-my-function/18624/5 "2019-11-29T23:35:33Z")

</div>

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