# Comparing a string

**URL:** <https://discourse.nodered.org/t/comparing-a-string/59693>\
**Category:** General\
**Created:** [11 March 2022 08:16 UTC](https://discourse.nodered.org/t/comparing-a-string/59693 "2022-03-11T08:16:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Milanovic](https://avatars.discourse-cdn.com/v4/letter/m/3da27b/32.png) [@Milanovic](https://discourse.nodered.org/u/Milanovic)\
**Post date:** [11 March 2022 08:17 UTC](https://discourse.nodered.org/t/comparing-a-string/59693/1 "2022-03-11T08:17:00Z")

</div>

Hi

I want to compare the variable I with an other number in string form. But it doesn't work  
Where is my fault?

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

---

<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:** [11 March 2022 08:30 UTC](https://discourse.nodered.org/t/comparing-a-string/59693/2 "2022-03-11T08:30:40Z")

</div>

What is the problem you are seeing? If it is always 1 at the end then that is because you set I to 1, test it for 1 and set it to 2. Then you test it for 2 (which it now is) and set it to 3. Then you test it for 3 (which it now is) and set it back to 1 again.

Perhaps you meant to use `else` on the later tests

---

<div class="post-metadata">

**Author:** ![Milanovic](https://avatars.discourse-cdn.com/v4/letter/m/3da27b/32.png) [@Milanovic](https://discourse.nodered.org/u/Milanovic)\
**Post date:** [11 March 2022 08:34 UTC](https://discourse.nodered.org/t/comparing-a-string/59693/3 "2022-03-11T08:34:59Z")

</div>

Yes, I have it already

But I want that the part I == 2 start on a second pulse of the timestamp. So on the first pulse, the first if loop en on the second pulse the second if loop.  
Now if I try it he does all the loops in one time

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/a/ca914b5f38b246dbf39e9d7aa0cea30d576fa5e4.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:** [11 March 2022 08:39 UTC](https://discourse.nodered.org/t/comparing-a-string/59693/4 "2022-03-11T08:39:44Z")

</div>

So you essentially want a payload of "1" then "2" then "3" and then back to "1" every time it runs - is that correct?

If YES, then what you must realise is that every time a function runs, it does exactly what you tell it to. You tell `I` to be `"1"` at the start of the function. You need to store the value of `I` somewhere at the end of the function then retrieve `I` at the beginning. To achieve this, you can use `context.set` and `context.get` See the docs for context: [Working with context : Node-RED](https://nodered.org/docs/user-guide/context)

---

<div class="post-metadata">

**Author:** ![Milanovic](https://avatars.discourse-cdn.com/v4/letter/m/3da27b/32.png) [@Milanovic](https://discourse.nodered.org/u/Milanovic)\
**Post date:** [11 March 2022 08:41 UTC](https://discourse.nodered.org/t/comparing-a-string/59693/5 "2022-03-11T08:41:30Z")

</div>

Is it possible to say: if I == 2 and Array[0][1] contains a value?

---

<div class="post-metadata">

**Author:** ![Milanovic](https://avatars.discourse-cdn.com/v4/letter/m/3da27b/32.png) [@Milanovic](https://discourse.nodered.org/u/Milanovic)\
**Post date:** [11 March 2022 08:44 UTC](https://discourse.nodered.org/t/comparing-a-string/59693/6 "2022-03-11T08:44:17Z")

</div>

So I will try to say what I am trying.  
I read every 5 minutes a value, but after 10 minutes (or 2 values) I need to calculate how much power I can use in the last 5 minutes.  
How can I do that?

---

<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:** [12 March 2022 08:38 UTC](https://discourse.nodered.org/t/comparing-a-string/59693/7 "2022-03-12T08:38:30Z")

</div>

> [@Milanovic](#):
>
> I need to calculate how much power I can use in the last 5 minutes

Do you know the formula you are trying to evaluate?

---

<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:** [11 May 2022 08:38 UTC](https://discourse.nodered.org/t/comparing-a-string/59693/8 "2022-05-11T08:38:48Z")

</div>

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