# How to access msg content and use "if"

**URL:** https://discourse.nodered.org/t/how-to-access-msg-content-and-use-if/33475
**Category:** General
**Created:** [28 September 2020 21:21 UTC](https://discourse.nodered.org/t/how-to-access-msg-content-and-use-if/33475 "2020-09-28T21:21:09Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![loeten](https://avatars.discourse-cdn.com/v4/letter/l/b38774/32.png) [@loeten](https://discourse.nodered.org/u/loeten)
#### Post date: [28 September 2020 21:21 UTC](https://discourse.nodered.org/t/how-to-access-msg-content-and-use-if/33475/1 "2020-09-28T21:21:09Z")

</div>

Hi there, I did not find a proper tutorial for this yet:

I have an IR sensor that gives this json via MQTT:

```auto
zigbee2mqtt/IrSensor : msg.payload : string[104]
"{"battery":100,"illuminance":26,"illuminance_lux":26,"linkquality":102,"occupancy":false,"voltage":3035}"

```

depending on occupacy I want to send a telegram message, but I always get the "Alarm over" no matter how occupacy is set. I guess I use the if incorrectly. Function content:

```auto
let occupancy = msg.payload.occupancy;
let payload = {
    "chatId":390475709,
    "type":"message",
    "content":"init"
};

if (occupancy === true) {
    payload.content = "IR Alarm aktiv"
} else {
    payload.content = "IR Alarm over"
}
return {payload};

```

How to do it right? And why? And where to learn this? 🙂

---

<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: [28 September 2020 21:31 UTC](https://discourse.nodered.org/t/how-to-access-msg-content-and-use-if/33475/2 "2020-09-28T21:31:26Z")

</div>

My first take on it would be that `occupancy` is **not** `true`.

Your code is:

```auto
if (occupancy === true) {
    payload.content = "IR Alarm aktiv"
} else {
    payload.content = "IR Alarm over"
}

```

So **ONLY IF** `occupancy == true` do you get the first message.

Try this code and see what it says:

```auto
let occupancy = msg.payload.occupancy;

node.warn(occupancy; // This will show you what `occupancy` is set to.

let payload = {
    "chatId":390475709,
    "type":"message",
    "content":"init"
};

if (occupancy === true) {
    payload.content = "IR Alarm aktiv"
} else {
    payload.content = "IR Alarm over"
}
return {payload};

```

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [28 September 2020 21:31 UTC](https://discourse.nodered.org/t/how-to-access-msg-content-and-use-if/33475/3 "2020-09-28T21:31:59Z")

</div>

I would start by reading - [https://nodered.org/docs/user-guide/messages](https://nodered.org/docs/user-guide/messages)

to understand how messages are made up and can be accessed.

In this case you are receiving a string - you really need a JSON object - so you can either set the MQTT node to return a parsed object for you - then feed into a debug node to see what's going on - or feed into a JSON node (that will do the same thing) - then to a debug node for you to inspect...

---

<div class="post-metadata">

### Author: ![loeten](https://avatars.discourse-cdn.com/v4/letter/l/b38774/32.png) [@loeten](https://discourse.nodered.org/u/loeten)
#### Post date: [29 September 2020 09:27 UTC](https://discourse.nodered.org/t/how-to-access-msg-content-and-use-if/33475/4 "2020-09-29T09:27:53Z")

</div>

> [@Trying\_to\_learn](#):
>
> ```auto
> node.warn(occupancy; // This will show you what `occupancy` is set to.
> 
> ```

Thank you, now I know how to debug better. It gives:

```auto
29.9.2020, 11:26:44node: SetTelegramMessagefunction : (warn)
undefined

```

---

<div class="post-metadata">

### Author: ![loeten](https://avatars.discourse-cdn.com/v4/letter/l/b38774/32.png) [@loeten](https://discourse.nodered.org/u/loeten)
#### Post date: [29 September 2020 09:28 UTC](https://discourse.nodered.org/t/how-to-access-msg-content-and-use-if/33475/5 "2020-09-29T09:28:58Z")

</div>

> [@dceejay](#):
>
> to understand how messages are made up and can be accessed.
> 
> In this case you are receiving a string - you really need a JSON object

Thank you, seems to make a lot of sense. Will do so.

---

<div class="post-metadata">

### Author: ![loeten](https://avatars.discourse-cdn.com/v4/letter/l/b38774/32.png) [@loeten](https://discourse.nodered.org/u/loeten)
#### Post date: [29 September 2020 09:32 UTC](https://discourse.nodered.org/t/how-to-access-msg-content-and-use-if/33475/6 "2020-09-29T09:32:23Z")

</div>

I changed the mqtt output to JSON object - now I get true or false in the debug as it should be. 👍

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [29 September 2020 10:46 UTC](https://discourse.nodered.org/t/how-to-access-msg-content-and-use-if/33475/7 "2020-09-29T10:46:45Z")

</div>

> [@loeten](#):
>
> I changed the mqtt output to JSON object

For clarity, you didn't change it to output JSON, you had JSON before (which is a string). You changed it so that it parses the JSON and converts it to a javascript object.

---

<div class="post-metadata">

### Author: ![loeten](https://avatars.discourse-cdn.com/v4/letter/l/b38774/32.png) [@loeten](https://discourse.nodered.org/u/loeten)
#### Post date: [29 September 2020 14:27 UTC](https://discourse.nodered.org/t/how-to-access-msg-content-and-use-if/33475/8 "2020-09-29T14:27:13Z")

</div>

> [@loeten](#):
>
> JSON object

sure. Just in the node settings it says "a parsed JSON object" - thats what I wanted to tell. But I clearly see the difference between the objcect and the string expression now.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [29 September 2020 14:41 UTC](https://discourse.nodered.org/t/how-to-access-msg-content-and-use-if/33475/9 "2020-09-29T14:41:54Z")

</div>

> [@loeten](#):
>
> a parsed JSON object

Yes, that is a bit ambiguous, it is easy to read it as outputting a JSON object, but actually it means a (JavaScript) Object created by parsing the JSON. Difficult to get complex meaning in a few words I suppose.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [28 November 2020 14:41 UTC](https://discourse.nodered.org/t/how-to-access-msg-content-and-use-if/33475/10 "2020-11-28T14:41:57Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
