# Waiting for a delta SP - Best Practice

**URL:** <https://discourse.nodered.org/t/waiting-for-a-delta-sp-best-practice/49585>\
**Category:** Industrial\
**Created:** [10 August 2021 22:13 UTC](https://discourse.nodered.org/t/waiting-for-a-delta-sp-best-practice/49585 "2021-08-10T22:13:10Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![wpmccormick](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/wpmccormick/32/46363_2.png) [@wpmccormick](https://discourse.nodered.org/u/wpmccormick)\
**Post date:** [10 August 2021 22:13 UTC](https://discourse.nodered.org/t/waiting-for-a-delta-sp-best-practice/49585/1 "2021-08-10T22:13:10Z")

</div>

I'm looking for some guidance; best practice sort of thing.

My application will be reading a changing analog value, and will need to make some decision (state change) when the change reaches some set-point.

I'm using the awesome smxstate, so I'm partial to something folds nicely into that framework.

My first thought is to have a dedicated flow update a global context value using a interval trigger, and then in my smxstate flow, have an activity that calculates the latest delta and update some other context.

If that all seems like the right direction, then great. If there's some more _formal_ Node-REDish way to do it, I'd love to hear about it.

Cheers!

---

<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 August 2021 07:10 UTC](https://discourse.nodered.org/t/waiting-for-a-delta-sp-best-practice/49585/2 "2021-08-11T07:10:14Z")

</div>

It isn't a formal node-red way, but the principle of node-red is to pass information by messages. Unless there is a good reason to use context it is better to pass the information down the wires.

---

<div class="post-metadata">

**Author:** ![wpmccormick](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/wpmccormick/32/46363_2.png) [@wpmccormick](https://discourse.nodered.org/u/wpmccormick)\
**Post date:** [11 August 2021 13:38 UTC](https://discourse.nodered.org/t/waiting-for-a-delta-sp-best-practice/49585/3 "2021-08-11T13:38:26Z")

</div>

Understood, but when dealing with history (as is the case when making a delta calculation), is there some way to handle that "down the wires", as you say? Or isn't context the Node-RED way to deal with history?

---

<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 August 2021 14:23 UTC](https://discourse.nodered.org/t/waiting-for-a-delta-sp-best-practice/49585/4 "2021-08-11T14:23:55Z")

</div>

Can you give a bit more detail on exactly what you want to do? With example data and the sequence of what happens?

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [11 August 2021 15:43 UTC](https://discourse.nodered.org/t/waiting-for-a-delta-sp-best-practice/49585/5 "2021-08-11T15:43:30Z")

</div>

> [@wpmccormick](#):
>
> best practice sort of thing

Here are a few possibly helpful thoughts. Data flow languages like Node-RED often struggle a bit with tasks that require memory of previous events, so there is no perfect solution. There are several contributed state machine nodes in addition to `smxstate`, but they have (at least for me) a learning curve to program and can be overkill for simple problems. Using a context variable in a `function` or `change` node is often good enough. The "Node-RED way" is generally to limit the scope of context variables as much as possible: use node rather than flow rather than global context. Finally, there are core nodes that offer small scraps of special-purpose memory. It your case, it might be worth looking at the `filter` (formerly `rbe`) node. It can pass only messages where some property has changed by a specified amount or percentage, and so might help to detect the changes you want.

---

<div class="post-metadata">

**Author:** ![wpmccormick](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/wpmccormick/32/46363_2.png) [@wpmccormick](https://discourse.nodered.org/u/wpmccormick)\
**Post date:** [11 August 2021 15:51 UTC](https://discourse.nodered.org/t/waiting-for-a-delta-sp-best-practice/49585/6 "2021-08-11T15:51:50Z")

</div>

I think the @drmibell reply is what I was looking for.

But think of it like this: I want to control a discrete output based on some change in an analog input.

So: output = analog\_change \< change\_setpoint

where analog\_change = analag\_current - analog\_startval and change\_setpoint is given.

---

<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 August 2021 16:10 UTC](https://discourse.nodered.org/t/waiting-for-a-delta-sp-best-practice/49585/7 "2021-08-11T16:10:25Z")

</div>

What do you mean by startval? The value when node-red is started? The previous sample?  
How is the change\_setpoint given? A fixed value? From dashboard input? Something else.

---

<div class="post-metadata">

**Author:** ![wpmccormick](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/wpmccormick/32/46363_2.png) [@wpmccormick](https://discourse.nodered.org/u/wpmccormick)\
**Post date:** [11 August 2021 16:18 UTC](https://discourse.nodered.org/t/waiting-for-a-delta-sp-best-practice/49585/8 "2021-08-11T16:18:31Z")

</div>

startval is some previous value.

setpoint is given through some message before startval is set.

---

<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 August 2021 16:54 UTC](https://discourse.nodered.org/t/waiting-for-a-delta-sp-best-practice/49585/9 "2021-08-11T16:54:13Z")

</div>

You can use a Join node to get all the values into one message (startval, setpoint and current value) then a simple bit of javascript can compare them and determine what to output. Make sure the input messages have different topic values and set the join node to manual mode generating an key/value object. Set it to output after three message types and after every subsequent message.  
Feed that into a function node which ignores any messages where the topic is not the current value topic (which means a new current value has just been received, and if it is the right topic then compare the values and output the appropriate message.

Build the above flow node by node and check each bit works as expected.

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

---

<div class="post-metadata">

**Author:** ![wpmccormick](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/wpmccormick/32/46363_2.png) [@wpmccormick](https://discourse.nodered.org/u/wpmccormick)\
**Post date:** [12 August 2021 02:47 UTC](https://discourse.nodered.org/t/waiting-for-a-delta-sp-best-practice/49585/10 "2021-08-12T02:47:31Z")

</div>

Running on my Pi, I still seem to have the _rbe_ node, and it would be a lot more useful if the deadband amount could be set dynamically from the input message.

Maybe the _filter_ node can do this? Or maybe _rbe_ can as well but it's not documented?

---

<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 August 2021 07:22 UTC](https://discourse.nodered.org/t/waiting-for-a-delta-sp-best-practice/49585/11 "2021-08-12T07:22:22Z")

</div>

The filter node is just the rbe node renamed, as the term rbe had caused confusion.

---

<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 August 2021 07:23 UTC](https://discourse.nodered.org/t/waiting-for-a-delta-sp-best-practice/49585/12 "2021-08-12T07:23:49Z")

</div>

Maybe node-red-contrib-hysteresis would be useful to you.

---

<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 October 2021 07:24 UTC](https://discourse.nodered.org/t/waiting-for-a-delta-sp-best-practice/49585/13 "2021-10-11T07:24:02Z")

</div>

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