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

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

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}]

Or set the chart to step mode ?

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?

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

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

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.

You can save the values in the context, as I mentioned.
Have a look at the node-red docs page on Working with Context.

Thank you, will work on it and get back!

Re the chart what I meant was set
image
As that will then render as steps rather than the diagonal lines - so will look like what you want.

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

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!

[{"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.

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