# String compare with state home assistant

**URL:** <https://discourse.nodered.org/t/string-compare-with-state-home-assistant/56366>\
**Category:** Developing Nodes\
**Created:** [9 January 2022 23:58 UTC](https://discourse.nodered.org/t/string-compare-with-state-home-assistant/56366 "2022-01-09T23:58:58Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Whastie](https://avatars.discourse-cdn.com/v4/letter/w/5fc32e/32.png) [@Whastie](https://discourse.nodered.org/u/Whastie)\
**Post date:** [9 January 2022 23:58 UTC](https://discourse.nodered.org/t/string-compare-with-state-home-assistant/56366/1 "2022-01-09T23:58:58Z")

</div>

Hello

Can someone help me see why the case that compares these strings does not work for me?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/7/f74866dd7414d128151c6bbca33608154f700275.png)

Thank you

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [10 January 2022 00:03 UTC](https://discourse.nodered.org/t/string-compare-with-state-home-assistant/56366/2 "2022-01-10T00:03:40Z")

</div>

Your arrowed debug is showing what is in `msg.payload` not `msg.payload.Modo_Operativo`

Add `node.warn(msg.payload)` and `node.warn(msg.payload.Modo_Operativo)` to your function you will see why this is happening.

---

<div class="post-metadata">

**Author:** ![Whastie](https://avatars.discourse-cdn.com/v4/letter/w/5fc32e/32.png) [@Whastie](https://discourse.nodered.org/u/Whastie)\
**Post date:** [10 January 2022 07:01 UTC](https://discourse.nodered.org/t/string-compare-with-state-home-assistant/56366/3 "2022-01-10T07:01:10Z")

</div>

Thank you very much for the help

I have tried what you tell me and now I get this error,  
I have to install a module or have I put something wrong?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/1/c1d22282db6523d39a65e84c4840d01d985617e1.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/a/da57bb4737bd479d7a9b0398e12f359ee6c2c24d.png)

Thank you

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [10 January 2022 08:01 UTC](https://discourse.nodered.org/t/string-compare-with-state-home-assistant/56366/4 "2022-01-10T08:01:14Z")

</div>

Firstly, I dont know why you are setting `var Modo` to the result of `node.warn`. `node.warn` is a debugging function.

Anyhow...

Do you see how `msg.payload.Modo_Operativo` is reported as being `undefined`?  
Do you see how `msg.payload` is reported as being `"Modo_Auto"`?

Use `msg.payload` instead of `msg.payload.Modo_Operativo` in your function...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/7/2797c0ab7066f6ef88d598b4830615f11b2d81a1.png)

* * *

### NOTE

Please paste function code as text NOT as an image.  
And always format code between 3 backticks  
`````  
`like this`  
`````

---

<div class="post-metadata">

**Author:** ![Whastie](https://avatars.discourse-cdn.com/v4/letter/w/5fc32e/32.png) [@Whastie](https://discourse.nodered.org/u/Whastie)\
**Post date:** [10 January 2022 08:27 UTC](https://discourse.nodered.org/t/string-compare-with-state-home-assistant/56366/5 "2022-01-10T08:27:03Z")

</div>

Hi

Ok, that part already works for me !!!

Now I need to collect the value and match it to the variable depending on each case

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/f/df41d3807e06e92f58c261a2e0d8d3f83d3fe45e.png)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [10 January 2022 08:43 UTC](https://discourse.nodered.org/t/string-compare-with-state-home-assistant/56366/6 "2022-01-10T08:43:53Z")

</div>

You cannot use the payload from completely different message.

Either store the value that comes out of the other HA node in flow/global context then you can access it later on in the function node using `flow.get`

Or

Use a join node to join the 2 messages from the 2 HA nodes.

* * *

#### some reading to help you...

See [this article in the cookbook](https://cookbook.nodered.org/basic/join-streams) for an example of how to join messages into one object.

* * *

There’s a great page in the docs that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

![BX00Cy7yHi](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd1f333a9e043b061b39917c328b0094d3e52d30.gif)

> **[Working with messages : Node-RED](https://nodered.org/docs/user-guide/messages)**

---

<div class="post-metadata">

**Author:** ![Whastie](https://avatars.discourse-cdn.com/v4/letter/w/5fc32e/32.png) [@Whastie](https://discourse.nodered.org/u/Whastie)\
**Post date:** [10 January 2022 09:12 UTC](https://discourse.nodered.org/t/string-compare-with-state-home-assistant/56366/7 "2022-01-10T09:12:28Z")

</div>

Hi

I have tried this function, but I guess I am missing something. It keeps appearing to me undefined.

thank you so much for help me

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/f/dffd3b81948c7369767900197eee029448dd55c8.png)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [10 January 2022 11:48 UTC](https://discourse.nodered.org/t/string-compare-with-state-home-assistant/56366/8 "2022-01-10T11:48:14Z")

</div>

You need to grab the stored context value from flow

E.g...

```auto
var value = flow.get("xxx")

```

Where `xxx` is your flow variable name.

* * *

I recommend you read the docs (linked above) particularly about function nodes.

Also, I recommend watching this playlist: [Node-RED Essentials](https://www.youtube.com/playlist?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6). The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

---

<div class="post-metadata">

**Author:** ![Whastie](https://avatars.discourse-cdn.com/v4/letter/w/5fc32e/32.png) [@Whastie](https://discourse.nodered.org/u/Whastie)\
**Post date:** [10 January 2022 11:54 UTC](https://discourse.nodered.org/t/string-compare-with-state-home-assistant/56366/9 "2022-01-10T11:54:49Z")

</div>

Thanks a lot !!

With this advice I have already solved it.

I will follow your advice and watch the tutorial videos

Thank you so much for everything !!

---

<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:** [24 January 2022 11:55 UTC](https://discourse.nodered.org/t/string-compare-with-state-home-assistant/56366/10 "2022-01-24T11:55:33Z")

</div>

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