# Seemingly trivial calculation does not work

**URL:** <https://discourse.nodered.org/t/seemingly-trivial-calculation-does-not-work/28465>\
**Category:** General\
**Created:** [17 June 2020 11:13 UTC](https://discourse.nodered.org/t/seemingly-trivial-calculation-does-not-work/28465 "2020-06-17T11:13:52Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![mcaklein](https://avatars.discourse-cdn.com/v4/letter/m/9d8465/32.png) [@mcaklein](https://discourse.nodered.org/u/mcaklein)\
**Post date:** [17 June 2020 11:13 UTC](https://discourse.nodered.org/t/seemingly-trivial-calculation-does-not-work/28465/1 "2020-06-17T11:13:52Z")

</div>

Hi all,

I measure the yield of my solar panels and created a Telegram interface. However, there is one problem I'm not able to solve...

This is the situation. The incoming MQTT message contains the total yield and I want to report the daily yield. So what I did: I created a trigger every day at 1:00 AM to set "flow.daystart" to the current total yield. This works. Then, when a new message is coming in, I try to set flow.dailyyield to the expression "msg.payload.yield - flow.daystart". However, this does not work, the flow.dailyyield is empty. If I adapt the change node to set flow.dailyyield to a numerical value (or to msg.payload.yield), there is no problem in using the value in the telegram message.

I cannot find out what is the cause. It seems that the trivial JSONata expression is not working. Do you have a clue what can be the problem?

Michel

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [17 June 2020 11:35 UTC](https://discourse.nodered.org/t/seemingly-trivial-calculation-does-not-work/28465/2 "2020-06-17T11:35:16Z")

</div>

Is msg.payload.yield a number or a string?

---

<div class="post-metadata">

**Author:** ![mcaklein](https://avatars.discourse-cdn.com/v4/letter/m/9d8465/32.png) [@mcaklein](https://discourse.nodered.org/u/mcaklein)\
**Post date:** [17 June 2020 12:02 UTC](https://discourse.nodered.org/t/seemingly-trivial-calculation-does-not-work/28465/3 "2020-06-17T12:02:38Z")

</div>

It was a string, but I converted it to a number in the following way:

```auto
$number(msg.payload.yield)/1000

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [17 June 2020 12:21 UTC](https://discourse.nodered.org/t/seemingly-trivial-calculation-does-not-work/28465/4 "2020-06-17T12:21:41Z")

</div>

Did you also convert flow.daystart to a number when you saved it to flow context?

---

<div class="post-metadata">

**Author:** ![mcaklein](https://avatars.discourse-cdn.com/v4/letter/m/9d8465/32.png) [@mcaklein](https://discourse.nodered.org/u/mcaklein)\
**Post date:** [17 June 2020 12:26 UTC](https://discourse.nodered.org/t/seemingly-trivial-calculation-does-not-work/28465/5 "2020-06-17T12:26:40Z")

</div>

Indirecty, as I set flow.daystart to the converted yield value.(For every MQTT message that comes in, I store flow.yield via above expression, and at 1:00 AM I set flow.daystart to flow.yield.)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [17 June 2020 12:36 UTC](https://discourse.nodered.org/t/seemingly-trivial-calculation-does-not-work/28465/6 "2020-06-17T12:36:19Z")

</div>

Can we see the output debug message.

---

<div class="post-metadata">

**Author:** ![mcaklein](https://avatars.discourse-cdn.com/v4/letter/m/9d8465/32.png) [@mcaklein](https://discourse.nodered.org/u/mcaklein)\
**Post date:** [17 June 2020 12:54 UTC](https://discourse.nodered.org/t/seemingly-trivial-calculation-does-not-work/28465/7 "2020-06-17T12:54:25Z")

</div>

That might be part of my problem, when I debug the "changed" message, I do not see what has changed in the flow variables. This is the output:

```auto
{"topic":"domoticz/out","payload":{"Battery":255,"RSSI":12,"description":"","dtype":"General","hwid":"11","id":"00082040","idx":40,"name":"Actual PV output","nvalue":0,"stype":"kWh","power":"3535.1","yield":"7091108","unit":1},"qos":0,"retain":false,"_msgid":"d0f6a611.a93bf8"}

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [17 June 2020 13:01 UTC](https://discourse.nodered.org/t/seemingly-trivial-calculation-does-not-work/28465/8 "2020-06-17T13:01:44Z")

</div>

> [@mcaklein](#):
>
> I try to set flow.dailyyield to the expression "msg.payload.yield - flow.daystart".

In this expression do you convert msg.payload.yield to a number again?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [17 June 2020 13:03 UTC](https://discourse.nodered.org/t/seemingly-trivial-calculation-does-not-work/28465/9 "2020-06-17T13:03:02Z")

</div>

you can see the flow variable in the context window.

---

<div class="post-metadata">

**Author:** ![mcaklein](https://avatars.discourse-cdn.com/v4/letter/m/9d8465/32.png) [@mcaklein](https://discourse.nodered.org/u/mcaklein)\
**Post date:** [17 June 2020 14:06 UTC](https://discourse.nodered.org/t/seemingly-trivial-calculation-does-not-work/28465/10 "2020-06-17T14:06:38Z")

</div>

It is missing (opbrengst = yield, startopbrengst = daystart):  
 ![afbeelding](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/8/0893a0fc6db0141ba96d907e0a3cf3698d23e666.png)

while this is the rule:  
 ![afbeelding](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/c/6cf8007682c6aead86c793f250095067b053c246.png)

(Thank you for all the help so far!)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [17 June 2020 14:20 UTC](https://discourse.nodered.org/t/seemingly-trivial-calculation-does-not-work/28465/11 "2020-06-17T14:20:46Z")

</div>

Does the output of the previuos node have a msg.payload.svalue2 ?

---

<div class="post-metadata">

**Author:** ![mcaklein](https://avatars.discourse-cdn.com/v4/letter/m/9d8465/32.png) [@mcaklein](https://discourse.nodered.org/u/mcaklein)\
**Post date:** [17 June 2020 14:43 UTC](https://discourse.nodered.org/t/seemingly-trivial-calculation-does-not-work/28465/12 "2020-06-17T14:43:13Z")

</div>

Yes (I referred to it as yield in the previous explanation). I'm certain about it, because I store the flow.yield value in the same change node:  
 ![afbeelding](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/8/e840350006e5200b24bbc48f3a3e455d82fc1419.png)

The first depicted rule reads:

```auto
$number(msg.payload.svalue2)/1000

```

and the second:

```auto
$number(msg.payload.svalue2)/1000-flow.startopbrengst

```

As you can see, the flow.startopbrengst has a value, but there is no flow.dagopbrengst generated.

---

<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:** [17 June 2020 14:46 UTC](https://discourse.nodered.org/t/seemingly-trivial-calculation-does-not-work/28465/13 "2020-06-17T14:46:48Z")

</div>

Simplify the rule, initially just setting it to svalue2 and make sure that works, then work up.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [17 June 2020 14:47 UTC](https://discourse.nodered.org/t/seemingly-trivial-calculation-does-not-work/28465/14 "2020-06-17T14:47:03Z")

</div>

I asked as the data will not be written to flow if it does not exist. The other flow data you see may be previous data. Post the debug of the previous node please.

---

<div class="post-metadata">

**Author:** ![mcaklein](https://avatars.discourse-cdn.com/v4/letter/m/9d8465/32.png) [@mcaklein](https://discourse.nodered.org/u/mcaklein)\
**Post date:** [17 June 2020 15:02 UTC](https://discourse.nodered.org/t/seemingly-trivial-calculation-does-not-work/28465/15 "2020-06-17T15:02:05Z")

</div>

The daily trigger initializes the value to 0. When I press the inject, I see the following output:  
 ![afbeelding](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/f/7f719178b116c41bc3835981764b3896fe649b6c.png)  
after one incoming MQTT message, the value is gone!  
 ![afbeelding](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/c/fc345bfb79b98cb0568fc47546daedc42388e5a1.png)

---

<div class="post-metadata">

**Author:** ![mcaklein](https://avatars.discourse-cdn.com/v4/letter/m/9d8465/32.png) [@mcaklein](https://discourse.nodered.org/u/mcaklein)\
**Post date:** [17 June 2020 15:02 UTC](https://discourse.nodered.org/t/seemingly-trivial-calculation-does-not-work/28465/16 "2020-06-17T15:02:36Z")

</div>

The debug-output of the previous node (a switch to filter relevant messages) is:  
 ![afbeelding](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/0/8014003e3aee6611015b4befd708cb38d404d3e8.png)

---

<div class="post-metadata">

**Author:** ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)\
**Post date:** [17 June 2020 15:21 UTC](https://discourse.nodered.org/t/seemingly-trivial-calculation-does-not-work/28465/17 "2020-06-17T15:21:13Z")

</div>

> [@mcaklein](#):
>
> and the second:
> 
> ```auto
> $number(msg.payload.svalue2)/1000-flow.startopbrengst
> 
> ```
> 
> As you can see, the flow.startopbrengst has a value, but there is no flow.dagopbrengst generated.

Hi,

`flow.startopbrengst` is not the proper JSONata expression for accessing flow context variables.

I think it should be something like:

- `$flowContext("startopbrengst")` or
- `$flowContext("opbrengst")`

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [17 June 2020 15:25 UTC](https://discourse.nodered.org/t/seemingly-trivial-calculation-does-not-work/28465/18 "2020-06-17T15:25:59Z")

</div>

The flow value would only be removed if the msg.payload.svalue2 did not exisit in the payload. Is that debug output defiantly after the switch and before the change node.

Maybe you should post your flow export here.

---

<div class="post-metadata">

**Author:** ![mcaklein](https://avatars.discourse-cdn.com/v4/letter/m/9d8465/32.png) [@mcaklein](https://discourse.nodered.org/u/mcaklein)\
**Post date:** [17 June 2020 15:28 UTC](https://discourse.nodered.org/t/seemingly-trivial-calculation-does-not-work/28465/19 "2020-06-17T15:28:23Z")

</div>

Yes, this was the solution! I changed the expression to:

```auto
$number(msg.payload.svalue2)/1000-$flowContext("startopbrengst")

```

and now it works. E1cid, Jan, thanks for the help!

---

<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:** [1 July 2020 15:28 UTC](https://discourse.nodered.org/t/seemingly-trivial-calculation-does-not-work/28465/20 "2020-07-01T15:28:23Z")

</div>

Closed on 1 July 2020 15:28 UTC

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