# How to get the Hysteresis as a chart? and find Duty cycles?

**URL:** <https://discourse.nodered.org/t/how-to-get-the-hysteresis-as-a-chart-and-find-duty-cycles/49006>\
**Category:** General\
**Created:** [28 July 2021 09:27 UTC](https://discourse.nodered.org/t/how-to-get-the-hysteresis-as-a-chart-and-find-duty-cycles/49006 "2021-07-28T09:27:27Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![KrishnanRavichandran](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krishnanravichandran/32/44858_2.png) [@KrishnanRavichandran](https://discourse.nodered.org/u/KrishnanRavichandran)\
**Post date:** [28 July 2021 09:27 UTC](https://discourse.nodered.org/t/how-to-get-the-hysteresis-as-a-chart-and-find-duty-cycles/49006/1 "2021-07-28T09:27:27Z")

</div>

I have a system to control my valve based on the input from the sensor.

If the value of sensor is below 0.27, I need the valve to switch ON and below the value 0.25, I need it to switch OFF. I have used the Hysteresis to obtain this functionality.

But, I am trying to obtain a chart in the dhashboard to show the state of the Hysteresis. I get the following chart.

![1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/f/df8bef26832ccf09aa79f020e9a69cceb389d801.jpeg)

Instead of the slant line I want the charts to display the state until there is no change. For example:  
either 0 or 1 (On or off state). I am trying to write a function but not successful in retaining the previous state for the graph.

Also can I have a variable (counter) to count the number of the times the state has switched On and to check how long in a day that the valve was in ON state in a day?

I am new to prograamming and would be happy to get your suggestions and try things to get the result. Thank you for the reply.

type or paste code here  
[{"id":"5cc803a0.00daac","type":"inject","z":"19ee434e.bdaadd","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":".20","payloadType":"num","x":180,"y":220,"wires":[["44b50f76.5c776"]]},{"id":"d6bbbb38.1ea408","type":"inject","z":"19ee434e.bdaadd","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":".26","payloadType":"num","x":180,"y":340,"wires":[["44b50f76.5c776"]]},{"id":"28de5d46.326362","type":"inject","z":"19ee434e.bdaadd","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":".30","payloadType":"num","x":180,"y":460,"wires":[["44b50f76.5c776"]]},{"id":"44b50f76.5c776","type":"hysteresis","z":"19ee434e.bdaadd","name":"","ThresholdType":"fixed","ThresholdRising":".27","ThresholdFalling":".25","TopicThreshold":"","TopicCurrent":"","ThresholdDeltaRising":"","ThresholdDeltaFalling":"","InitialMessage":false,"OutRisingType":"num","OutRisingValue":"1","OutFallingType":"num","OutFallingValue":"0","OutTopicType":"top","OutTopicValue":"","DynRaiseError":false,"x":470,"y":340,"wires":[["896bcf1d.0aa85","65b0e7b9.1c3518","a62990d5.b73be"]]},{"id":"65b0e7b9.1c3518","type":"ui\_chart","z":"19ee434e.bdaadd","name":"","group":"3e9d2da9.93da42","order":0,"width":0,"height":0,"label":"chart","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"","ymax":"","removeOlder":"24","removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"useUTC":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"outputs":1,"useDifferentColor":false,"x":980,"y":300,"wires":[]},{"id":"896bcf1d.0aa85","type":"debug","z":"19ee434e.bdaadd","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":730,"y":260,"wires":},{"id":"3e9d2da9.93da42","type":"ui\_group","name":"Default","tab":"86a34ae4.73b6c8","order":1,"disp":true,"width":"6","collapse":false},{"id":"86a34ae4.73b6c8","type":"ui\_tab","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

---

<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:** [28 July 2021 09:39 UTC](https://discourse.nodered.org/t/how-to-get-the-hysteresis-as-a-chart-and-find-duty-cycles/49006/2 "2021-07-28T09:39:57Z")

</div>

Or set the chart to step mode ?

---

<div class="post-metadata">

**Author:** ![KrishnanRavichandran](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krishnanravichandran/32/44858_2.png) [@KrishnanRavichandran](https://discourse.nodered.org/u/KrishnanRavichandran)\
**Post date:** [28 July 2021 09:51 UTC](https://discourse.nodered.org/t/how-to-get-the-hysteresis-as-a-chart-and-find-duty-cycles/49006/3 "2021-07-28T09:51:14Z")

</div>

Thank you @dceejay !

Should I use a global variable to increase the count each time I reach the ON state?

and if I need to store the value of the state (On or OFF) for each second on a file using the file node, How could I retain the previous state?

---

<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:** [28 July 2021 10:12 UTC](https://discourse.nodered.org/t/how-to-get-the-hysteresis-as-a-chart-and-find-duty-cycles/49006/4 "2021-07-28T10:12:26Z")

</div>

> [@KrishnanRavichandran](#):
>
> Should I use a global variable to increase the count each time I reach the ON state

Is this a separate question to the chart question?  
Why do you want to keep a count?

> [@KrishnanRavichandran](#):
>
> if I need to store the value of the state (On or OFF) for each second on a file

Have a look at the node-red docs page on Using Context. That will show you how you can keep the most recent value.

---

<div class="post-metadata">

**Author:** ![KrishnanRavichandran](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krishnanravichandran/32/44858_2.png) [@KrishnanRavichandran](https://discourse.nodered.org/u/KrishnanRavichandran)\
**Post date:** [28 July 2021 10:32 UTC](https://discourse.nodered.org/t/how-to-get-the-hysteresis-as-a-chart-and-find-duty-cycles/49006/5 "2021-07-28T10:32:33Z")

</div>

It is with respect to my initial question.

The scenario I want to obtain is

1. for each time the hysteresis node provides a ON state, I want the to add it to the counter value. This is to know how many times in a day that the valve is switched on and off for the purpose of understanding if there is any leak in my sytem (in case if the count is drastically high)

2. I just need the state of the system for each second as displyed in the chart to be stored in the csv file. For eg: if the system is in On state from 12 30 till 12 40, then in the csv file I need the state to be obtained as the same. Similarly if the system switches to off from 12 40 till 13 00, this could be stored.

---

<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:** [28 July 2021 10:37 UTC](https://discourse.nodered.org/t/how-to-get-the-hysteresis-as-a-chart-and-find-duty-cycles/49006/6 "2021-07-28T10:37:18Z")

</div>

You can save the values in the context, as I mentioned.  
Have a look at the node-red docs page on [Working with Context](https://nodered.org/docs/user-guide/context).

---

<div class="post-metadata">

**Author:** ![KrishnanRavichandran](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krishnanravichandran/32/44858_2.png) [@KrishnanRavichandran](https://discourse.nodered.org/u/KrishnanRavichandran)\
**Post date:** [28 July 2021 12:07 UTC](https://discourse.nodered.org/t/how-to-get-the-hysteresis-as-a-chart-and-find-duty-cycles/49006/7 "2021-07-28T12:07:56Z")

</div>

Thank you, will work on it and get back!

---

<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:** [28 July 2021 12:22 UTC](https://discourse.nodered.org/t/how-to-get-the-hysteresis-as-a-chart-and-find-duty-cycles/49006/8 "2021-07-28T12:22:11Z")

</div>

Re the chart what I meant was set  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/8/38cde12b61565e0bae9f0aeb91e3d5f95b80715d.png)  
As that will then render as steps rather than the diagonal lines - so will look like what you want.

---

<div class="post-metadata">

**Author:** ![KrishnanRavichandran](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krishnanravichandran/32/44858_2.png) [@KrishnanRavichandran](https://discourse.nodered.org/u/KrishnanRavichandran)\
**Post date:** [28 July 2021 12:48 UTC](https://discourse.nodered.org/t/how-to-get-the-hysteresis-as-a-chart-and-find-duty-cycles/49006/9 "2021-07-28T12:48:51Z")

</div>

Ya I got this one immediately after you commented. I am working on the other two questions as per the suggestion from @Colin

---

<div class="post-metadata">

**Author:** ![KrishnanRavichandran](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krishnanravichandran/32/44858_2.png) [@KrishnanRavichandran](https://discourse.nodered.org/u/KrishnanRavichandran)\
**Post date:** [28 July 2021 14:38 UTC](https://discourse.nodered.org/t/how-to-get-the-hysteresis-as-a-chart-and-find-duty-cycles/49006/10 "2021-07-28T14:38:24Z")

</div>

With the help of context data, I was able to get the counter value increase for each time the ON state is received.  
I used the following code. I am new to programming and Node red, so if you have some suggestions I am willing to hear!

```auto
[{"id":"50f9a0c6.989ab","type":"inject","z":"19ee434e.bdaadd","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"00 00 * * *","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":380,"y":80,"wires":[["2b1d6efe.dec342"]]},{"id":"2b1d6efe.dec342","type":"change","z":"19ee434e.bdaadd","name":"","rules":[{"t":"set","p":"count","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":80,"wires":[["63485fbb.2e75b"]]},{"id":"63485fbb.2e75b","type":"debug","z":"19ee434e.bdaadd","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":830,"y":80,"wires":[]},{"id":"5cc803a0.00daac","type":"inject","z":"19ee434e.bdaadd","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":".20","payloadType":"num","x":180,"y":220,"wires":[["44b50f76.5c776"]]},{"id":"d6bbbb38.1ea408","type":"inject","z":"19ee434e.bdaadd","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":".26","payloadType":"num","x":180,"y":340,"wires":[["44b50f76.5c776"]]},{"id":"28de5d46.326362","type":"inject","z":"19ee434e.bdaadd","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":".30","payloadType":"num","x":180,"y":460,"wires":[["44b50f76.5c776"]]},{"id":"44b50f76.5c776","type":"hysteresis","z":"19ee434e.bdaadd","name":"","ThresholdType":"fixed","ThresholdRising":".27","ThresholdFalling":".25","TopicThreshold":"","TopicCurrent":"","ThresholdDeltaRising":"","ThresholdDeltaFalling":"","InitialMessage":false,"OutRisingType":"num","OutRisingValue":"1","OutFallingType":"num","OutFallingValue":"0","OutTopicType":"top","OutTopicValue":"","DynRaiseError":false,"x":440,"y":300,"wires":[["896bcf1d.0aa85","65b0e7b9.1c3518","a62990d5.b73be","7e7fc325.a9cdac"]]},{"id":"7e7fc325.a9cdac","type":"switch","z":"19ee434e.bdaadd","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"0","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":620,"y":460,"wires":[["22e3e5cc.eac42a"],[]]},{"id":"22e3e5cc.eac42a","type":"function","z":"19ee434e.bdaadd","name":"","func":"msg.payload = flow.get(\"count\")+ 1; \nflow.set(\"count\", msg.payload); \nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":780,"y":420,"wires":[["b6476edb.0c6d6"]]},{"id":"b6476edb.0c6d6","type":"debug","z":"19ee434e.bdaadd","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":990,"y":420,"wires":[]}]

```

I am now trying to save the data in csv when ever the state is changing along with the previous state. Hope to accomplish it.

---

<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:** [26 September 2021 14:38 UTC](https://discourse.nodered.org/t/how-to-get-the-hysteresis-as-a-chart-and-find-duty-cycles/49006/11 "2021-09-26T14:38:45Z")

</div>

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