Set the input signal to high and low for a while of 1 second

Dear Guys,
I am new with node-red as well as JavaScript.
I would like to make active a device for some percent of 1 second in 1000 ms (e.g. 60% of 1 second= 600ms) with high=1 and make the rest time (40% of 1000ms) inactive low=0. The flow shall be setting as a loop while the main button is true (button status= on or button status=1).
the flow in details:

  • Button is on.
  • A slide button is avaiable to setting at UI between 0..100 (0..100% of power).
  • The slide msg will be scaled from 0..100% to 0...1000 ms. (multiple to 10).
  • main loop is active:
  • the device is active for x% (between 1..1000 ms). msg forward to device is high=1;
  • the device is inactive for the rest time (between 1..1000 ms). The msg forward to device is low=0;
  • The loop is valid until main button is switched on.

Do you have idea how is the flow to make? Thank you for your help.

Do you mean you want a Pulse Width Modulated output? If so then the best way to do it will depend on what device you are running node red.

The Puls Width Modulation (PWM) is applicable with analog outputs (AO). I need the flow for controlling of a digital output that accept the value 0 =0V or 1=24V. In this regards I thought I can set the message for a while of one second (1000ms) to 1 or 0.
The main loop send input message periodic (as long as the main button is ON) set to 1 and 0. Is it possible?

No, pwm is for digital outputs.

You will need to explain what hardware you are using first.

I use 8 channel digital Input/out of Wago 750-1506. Information is avaiable under
[https://www.wago.com › io-systeme › 750-1506](https://www.wago.com › io-systeme › 750-1506)
This is not for pmw avaiable.

This flow should give you some ideas about how to do the part you call the "main loop".

[{"id":"c51c17bf.53f62","type":"inject","z":"79a5d728.2c41d8","name":"","topic":"","payload":"on","payloadType":"str","repeat":"1","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":240,"wires":[["1d88d123.c95f87","a76dd308.ace7"]]},{"id":"1d88d123.c95f87","type":"debug","z":"79a5d728.2c41d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":710,"y":240,"wires":[]},{"id":"64b85c6b.8060f4","type":"delay","z":"79a5d728.2c41d8","name":"delay","pauseType":"delayv","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":410,"y":200,"wires":[["a90b22e7.e8bcf8"]]},{"id":"a90b22e7.e8bcf8","type":"change","z":"79a5d728.2c41d8","name":"off","rules":[{"t":"set","p":"payload","pt":"msg","to":"off","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":530,"y":200,"wires":[["1d88d123.c95f87"]]},{"id":"f81b967f.3d1238","type":"inject","z":"79a5d728.2c41d8","name":"50%","topic":"","payload":".5","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":310,"y":80,"wires":[["72db358.a8df04c"]]},{"id":"a76dd308.ace7","type":"change","z":"79a5d728.2c41d8","name":"get delay","rules":[{"t":"set","p":"delay","pt":"msg","to":"delay","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":200,"wires":[["64b85c6b.8060f4"]]},{"id":"72db358.a8df04c","type":"change","z":"79a5d728.2c41d8","name":"set delay","rules":[{"t":"set","p":"delay","pt":"flow","to":"payload*1000","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":80,"wires":[[]]},{"id":"ba593648.89f988","type":"inject","z":"79a5d728.2c41d8","name":"30%","topic":"","payload":".3","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":310,"y":40,"wires":[["72db358.a8df04c"]]},{"id":"395bec86.36a504","type":"inject","z":"79a5d728.2c41d8","name":"70%","topic":"","payload":".7","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":310,"y":120,"wires":[["72db358.a8df04c"]]}]

You would have to replace the inject nodes with a slider to set the duty cycle and figure out where to put the "main button." Good luck with your project.

Hi, thanks a lot for your code. It seems very compact and effectively. I close it to my main flow and report about the result.

hi drmibell,
I used your code in my flows. you see attached the complete flow. The flow functions, but not as I expect.
I made some comment on some node to underestanding what I mean. In principle, the following conditions of flow are to be expected:

  • flow sends message only if main switch is set to ON
  • flow sends the values 0 and 1 in % of value from slide (1..100%)
  • Flow repeats the interval as long as the main switch is ON and the percentage is greater than 0.

How can we adjust the flow to work properly? My sincerity is now the control of Intreval Inject. How can we set this up so that it works only if button = ON. The flow:

remark: i made copy and paste the code. I hope it is workable!

remark: i made copy and paste the code. I hope it is workable!

It's not.

Export your flow again and post it like:

```
your code
```

[{"id":"e01cd0e3.b9bbf8","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"b10f794c.605748","type":"ui_slider","z":"e01cd0e3.b9bbf8","name":"Heater value 0..100%","label":"Heater value 0..100%","tooltip":"","group":"56943ca1.7f3dac","order":3,"width":"0","height":"0","passthru":true,"outs":"end","topic":"","min":0,"max":"100","step":"10","x":200,"y":220,"wires":[["1a41cd67.05e083","984e3223.cebca","c74b96a1.5c1508"]]},{"id":"34af1dda.e384aa","type":"function","z":"e01cd0e3.b9bbf8","name":"global get heater true/false","func":"var obtaindata = global.get (\"heater\")\nif (obtaindata == \"1\"){msg.payload = 0}\nif (obtaindata == \"0\"){msg.payload = 1}\nreturn msg;","outputs":1,"noerr":0,"x":200,"y":140,"wires":[["efbb72d.db7429","b10f794c.605748"]]},{"id":"efbb72d.db7429","type":"debug","z":"e01cd0e3.b9bbf8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":790,"y":140,"wires":[]},{"id":"e72b9557.cb72f","type":"inject","z":"e01cd0e3.b9bbf8","name":"","topic":"","payload":"1","payloadType":"num","repeat":"1","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":400,"wires":[["924d9ae7.df8368"]]},{"id":"e7d294f5.f73048","type":"debug","z":"e01cd0e3.b9bbf8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":830,"y":460,"wires":[]},{"id":"27d97eeb.5ee842","type":"delay","z":"e01cd0e3.b9bbf8","name":"delay","pauseType":"delayv","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":530,"y":400,"wires":[["b8513d7c.f45898"]]},{"id":"b8513d7c.f45898","type":"change","z":"e01cd0e3.b9bbf8","name":"off (0)","rules":[{"t":"set","p":"payload","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":670,"y":400,"wires":[["e7d294f5.f73048","fb09be08.a2fd18"]]},{"id":"924d9ae7.df8368","type":"change","z":"e01cd0e3.b9bbf8","name":"get delay","rules":[{"t":"set","p":"delay","pt":"msg","to":"delay","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":400,"wires":[["27d97eeb.5ee842","38472853.2a9de8"]]},{"id":"12742810.7fc79","type":"change","z":"e01cd0e3.b9bbf8","name":"set delay","rules":[{"t":"set","p":"delay","pt":"flow","to":"payload*2000","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":780,"y":300,"wires":[[]]},{"id":"38472853.2a9de8","type":"debug","z":"e01cd0e3.b9bbf8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":550,"y":460,"wires":[]},{"id":"1a41cd67.05e083","type":"function","z":"e01cd0e3.b9bbf8","name":"gloabl.get (heater) =1 / msg.payload=0","func":"var obtaindata = global.get (\"heater\")\nif (obtaindata === 1){return msg}\nif (obtaindata === 0){msg.payload = 0}\nreturn msg;","outputs":1,"noerr":0,"x":230,"y":300,"wires":[["12742810.7fc79","924d9ae7.df8368"]]},{"id":"65adc14f.5a1a2","type":"ui_switch","z":"e01cd0e3.b9bbf8","name":"ON/OFF","label":"ON/OFF","tooltip":"","group":"56943ca1.7f3dac","order":2,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":220,"y":60,"wires":[["34af1dda.e384aa"]]},{"id":"fb09be08.a2fd18","type":"modbus-write","z":"e01cd0e3.b9bbf8","name":"ModbusTCP Write DO_03 (Q3)","showStatusActivities":true,"showErrors":false,"unitid":"1","dataType":"HoldingRegister","adr":"3","quantity":"1","server":"2312041.d3ca1fc","x":890,"y":400,"wires":[[],[]]},{"id":"984e3223.cebca","type":"debug","z":"e01cd0e3.b9bbf8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":430,"y":200,"wires":[]},{"id":"c74b96a1.5c1508","type":"ui_gauge","z":"e01cd0e3.b9bbf8","name":"heater gauge","group":"56943ca1.7f3dac","order":1,"width":0,"height":0,"gtype":"gage","title":"power","label":"% power","format":"{{value}}","min":0,"max":"100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":790,"y":220,"wires":[]},{"id":"e5aabfe3.b406b8","type":"comment","z":"e01cd0e3.b9bbf8","name":"Conditions of get delay","info":"get delay has to function, if global.heater=1 (is on)","x":340,"y":440,"wires":[]},{"id":"b6e2bf5c.975838","type":"comment","z":"e01cd0e3.b9bbf8","name":"Conditions of get delay","info":"set delay is active, if global.heater=1 (is on)\nand heater value is > 0;","x":520,"y":280,"wires":[]},{"id":"aaba54a0.ae42a","type":"comment","z":"e01cd0e3.b9bbf8","name":"Error on inject:","info":"If the inject is setting with interval of x secondes,\nthe flow send allways the 0 and 1 as message, regardless\nof whether the main switch is set to on or off","x":140,"y":440,"wires":[]},{"id":"56943ca1.7f3dac","type":"ui_group","z":"","name":"heater value","tab":"e8005803.ae17c8","order":1,"disp":false,"width":"8","collapse":false},{"id":"2312041.d3ca1fc","type":"modbus-client","z":"","name":"modbus-tcp@192.168.1.5","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":false,"tcpHost":"192.168.1.5","tcpPort":"502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":"1","commandDelay":"1","clientTimeout":"1000","reconnectTimeout":"1000"},{"id":"e8005803.ae17c8","type":"ui_tab","z":"","name":"Heater ON/OFF 2","icon":"dashboard","order":32,"disabled":false,"hidden":false}]

Sorry, incorrect again. (edit your post, don't post new comment)

Read this post

yes I see. I have export from node red in clipboard and then hier. I am very new in cmmnunity. how can import the flow code such as drmibell on the top as real json code?

thank you. I have done. see you please top.

Since you are new to NR and JS (but not programming, I hope), I tried to give you a head start on what seemed to the toughest bit. I think you will learn more by sorting out the rest of the logic yourself, but here are a few hints.

  • The delay is set by a flow context property, so there is no need for a connection between the part of the flow that sets the delay and the part that uses it. Here, it is actually causing a problem.

  • The delay node setting is in milliseconds, so 100% corresponds to flow.delay=1000.

  • You probably need to re-think the operation of the main ON/OFF switch. It could set a flow context that is checked by a node the "main loop", either near the input or at the output.

  • You also want to be sure that the device (heater?) goes to the correct state each time the main switch is thrown.

In general, when you upload flows to the forum to ask for troubleshooting help it is best, whenever possible, to eliminate contributed nodes (like the modbus nodes) that few users are likely to have downloaded. If these can be replaced by inject or debug nodes or omitted entirely, it makes it easier for people to help you. The same applies to a lesser extent to dashboard nodes. You may even find in your own development work that getting the message flow and program logic correct is easier if the user interface is mocked-up with inject and debug nodes. Then everything can be done in the editor, and the dashboard can usually be added later.

Good luck with your project.

Hi, I extended the flow by one range (instead of inject) and connected to device and tested. it works as I imagined it to be. I keep developing it. the flow looks like this:

[{"id":"85a28ca5.288778","type":"range","z":"e01cd0e3.b9bbf8","minin":"0","maxin":"100","minout":"0","maxout":"1","action":"scale","round":false,"property":"payload","name":"","x":370,"y":160,"wires":[["12742810.7fc79"]]}]

I used only standard node red functions. Thank you for help.

1 Like