# Heatmap - only update changed values

**URL:** <https://discourse.nodered.org/t/heatmap-only-update-changed-values/32342>\
**Category:** General\
**Created:** [2 September 2020 11:14 UTC](https://discourse.nodered.org/t/heatmap-only-update-changed-values/32342 "2020-09-02T11:14:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hermiot](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hermiot/32/28140_2.png) [@hermiot](https://discourse.nodered.org/u/hermiot)\
**Post date:** [2 September 2020 11:14 UTC](https://discourse.nodered.org/t/heatmap-only-update-changed-values/32342/1 "2020-09-02T11:14:37Z")

</div>

Hi,

I build a heatmap to monitore temperatures and CO2 values on a floor plan image.  
By design, the [Heatmap Node](https://flows.nodered.org/node/node-red-contrib-ui-heatmap) displays a given Matrix which works like a charm.

My sensors send their data via MQTT in different sample rates.  
I'm using a function to build the heatmap matrix

```auto
var T1 = msg.payload.U10_T1;
var T2 = msg.payload.U10_T2;

msg.payload = 
[
    T1, T2
];

return msg;

```

Because messages for T1 and T2 are asynchron, mostly one of both is _null_.

Does anyone has an idea, how to keep the values of T1 and T2 until they change?  
This would give me the possibility to display values of different sample rates on my heatmap.

Thank you 🙂

Martin

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [2 September 2020 12:16 UTC](https://discourse.nodered.org/t/heatmap-only-update-changed-values/32342/2 "2020-09-02T12:16:25Z")

</div>

What should happen the first time one of the readings comes - say T1, but T2 has never come in?

---

<div class="post-metadata">

**Author:** ![hermiot](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hermiot/32/28140_2.png) [@hermiot](https://discourse.nodered.org/u/hermiot)\
**Post date:** [3 September 2020 09:11 UTC](https://discourse.nodered.org/t/heatmap-only-update-changed-values/32342/3 "2020-09-03T09:11:30Z")

</div>

It doesn't matter. They can use a default like any number. This is where I stuck buildung an if statement which checks for new data.  
I didn't find a way to initialize variables and set them to a number value.

---

<div class="post-metadata">

**Author:** ![hermiot](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hermiot/32/28140_2.png) [@hermiot](https://discourse.nodered.org/u/hermiot)\
**Post date:** [4 September 2020 11:11 UTC](https://discourse.nodered.org/t/heatmap-only-update-changed-values/32342/4 "2020-09-04T11:11:58Z")

</div>

I got it working

```auto
var T1_temp = context.get('T1_temp')||1;
var T2_temp = context.get('T2_temp')||2;

var T1 = msg.payload.U10_T1;
var T2 = msg.payload.U10_T2;

if (T1 === undefined) {
   T1 = T1_temp;
   context.set('T1',T1_temp);
}

else {
    T1_temp = T1;
    context.set('T1_temp',T1);
}

if (T2 === undefined) {
   T2 = T2_temp;
   context.set('T2',T2_temp);
}

else {
    T2_temp = T2;
    context.set('T2_temp',T2);
}

msg.payload = 
[
    T1, T2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0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0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0

];

return msg;

```

But I think I need to learn something about callbacks. This seems to be a much nicer way...

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [4 September 2020 11:23 UTC](https://discourse.nodered.org/t/heatmap-only-update-changed-values/32342/5 "2020-09-04T11:23:13Z")

</div>

Hi Martin,

I have not tried it, but - out of my head - can't you shorten it to something like this:

```auto
var T1 = msg.payload.U10_T1||context.get('T1')||1;
var T2 = msg.payload.U10_T2||context.get('T2')||2;

context.set('T1',T1);
context.set('T2',T2);

```

I mean: first try to get it from the message, then try to get it from context memory, and otherwise use a default value?

Bart

---

<div class="post-metadata">

**Author:** ![hermiot](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hermiot/32/28140_2.png) [@hermiot](https://discourse.nodered.org/u/hermiot)\
**Post date:** [4 September 2020 11:26 UTC](https://discourse.nodered.org/t/heatmap-only-update-changed-values/32342/6 "2020-09-04T11:26:39Z")

</div>

This is fantastic!  
Thank you so much! 🤩

---

<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:** [18 September 2020 11:26 UTC](https://discourse.nodered.org/t/heatmap-only-update-changed-values/32342/7 "2020-09-18T11:26:49Z")

</div>

Closed on 18 September 2020 11:26 UTC

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