# Saw-tooth to rectangular function / create additional message

**URL:** https://discourse.nodered.org/t/saw-tooth-to-rectangular-function-create-additional-message/35519
**Category:** General
**Created:** [7 November 2020 19:49 UTC](https://discourse.nodered.org/t/saw-tooth-to-rectangular-function-create-additional-message/35519 "2020-11-07T19:49:15Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Detlev](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/detlev/32/28860_2.png) [@Detlev](https://discourse.nodered.org/u/Detlev)
#### Post date: [7 November 2020 19:49 UTC](https://discourse.nodered.org/t/saw-tooth-to-rectangular-function-create-additional-message/35519/1 "2020-11-07T19:49:15Z")

</div>

Sorry if this is a beginner question, but I have the following situation:

A KNX message is received everytime a binary switch changes its value, i.e. toggles between on and off. When displaying these on or off status in a dashboard line chart, obviously and correctly a saw-tooth pattern results, with diagonal lines between the 0 and the 1 state.

However, this does not reflect the binary nature and makes it difficult to immediately and intuitively see in which periodes the switch was on or off. What I want is a rectangular shape with a horizontal line at 1 for on and at 0 for off.

My thought was that I would simply need to add an additional data point each time a new state arrives. For example, the state is 0 since quite a time and a 1 is received, I would add a 0, so the line chart represents the old and the new 0 value and immediately after I would give the 1 resulting in the almost-vertical rise to state 1. Perfect representation of the switch values.

But how do I do this in NodeRed? I need a node that can create one additional message and passes on the original message very close after each other (otr maybe it works consecutive at the same time).

---

<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: [7 November 2020 20:20 UTC](https://discourse.nodered.org/t/saw-tooth-to-rectangular-function-create-additional-message/35519/2 "2020-11-07T20:20:16Z")

</div>

Assuming you are using the standard dashboard chart then set the Interpolate field in the config to Step.

---

<div class="post-metadata">

### Author: ![Detlev](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/detlev/32/28860_2.png) [@Detlev](https://discourse.nodered.org/u/Detlev)
#### Post date: [7 November 2020 20:55 UTC](https://discourse.nodered.org/t/saw-tooth-to-rectangular-function-create-additional-message/35519/3 "2020-11-07T20:55:28Z")

</div>

Wow, perfect, they even thought of this. Sorry for the stupid question, I didn't even had the idea that they might have programmed that. Fantastic! Solved.

But, anyway, would there have been a solution like mine? Duplicating message with changed payload?

---

<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: [7 November 2020 21:24 UTC](https://discourse.nodered.org/t/saw-tooth-to-rectangular-function-create-additional-message/35519/4 "2020-11-07T21:24:53Z")

</div>

Yes, you could do that in a function node, saving the previous value in the node context then each time a new value comes in send the previous one first. The node red docs page Writing Functions tells you how to use the node context.

---

<div class="post-metadata">

### Author: ![Detlev](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/detlev/32/28860_2.png) [@Detlev](https://discourse.nodered.org/u/Detlev)
#### Post date: [8 November 2020 08:43 UTC](https://discourse.nodered.org/t/saw-tooth-to-rectangular-function-create-additional-message/35519/5 "2020-11-08T08:43:45Z")

</div>

Hm, I can simply use then inverted state without memorising the previous state, but I cannot figure out how to make one function return two messages.

---

<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: [8 November 2020 08:50 UTC](https://discourse.nodered.org/t/saw-tooth-to-rectangular-function-create-additional-message/35519/6 "2020-11-08T08:50:29Z")

</div>

See the docs [https://nodered.org/docs/user-guide/writing-functions](https://nodered.org/docs/user-guide/writing-functions)

---

<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: [8 November 2020 09:04 UTC](https://discourse.nodered.org/t/saw-tooth-to-rectangular-function-create-additional-message/35519/7 "2020-11-08T09:04:26Z")

</div>

> [@Detlev](#):
>
> I can simply use then inverted state without memorising the previous state,

That is ok, provided you can absolutely guarantee you will only get a message on a state change which may be true in your case. Personally I wouldn't do that though, it is called defensive programming.

---

<div class="post-metadata">

### Author: ![Detlev](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/detlev/32/28860_2.png) [@Detlev](https://discourse.nodered.org/u/Detlev)
#### Post date: [8 November 2020 09:17 UTC](https://discourse.nodered.org/t/saw-tooth-to-rectangular-function-create-additional-message/35519/8 "2020-11-08T09:17:59Z")

</div>

Yeah, your are right and I agree from a programmer's point of view. However, in this case the KNX actor can be set to send a message only on change.

Anyway, your answer with "interpolate: step" solved then issue in a perfect way, so now I am just curious how I could produce two messages from one incoming message.

---

<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: [8 November 2020 09:26 UTC](https://discourse.nodered.org/t/saw-tooth-to-rectangular-function-create-additional-message/35519/9 "2020-11-08T09:26:30Z")

</div>

> [@Detlev](#):
>
> now I am just curious how I could produce two messages from one incoming message.

Have you read the guide Dave linked to?

---

<div class="post-metadata">

### Author: ![Detlev](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/detlev/32/28860_2.png) [@Detlev](https://discourse.nodered.org/u/Detlev)
#### Post date: [8 November 2020 09:51 UTC](https://discourse.nodered.org/t/saw-tooth-to-rectangular-function-create-additional-message/35519/10 "2020-11-08T09:51:57Z")

</div>

Sorry, got distracted by the "store in context" issue. Yes, I now processed the multiple output options. This solved the issue. I could implement everything I wanted.

Thanks again!

---

<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: [7 January 2021 09:52 UTC](https://discourse.nodered.org/t/saw-tooth-to-rectangular-function-create-additional-message/35519/11 "2021-01-07T09:52:05Z")

</div>

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