Javascript for PWM (Pulse Width Modulation) to dim a heater

Hello, NodeRed is new for me, and I'm new here....
I Installed Node-RED on a Unipi Neuron S103 (Raspberry Pi inside) and want to dim an electrical heater. I want to send short pulses (PWM) to a solid state relais. Can somebody write a PWM in JavaScript?

The GPIO Out node can be configured to output PWM. Or does that not do what what you want?

yes GPIO can do simple PWM but for a heater I think @colin 's PID control may be more appropriate :slight_smile: node-red-contrib-pid (node) - Node-RED depending on what the actual bigger requirement is.

Perhaps I misunderstood what you are trying to do. What frequency pwm are you trying to generate?

@colin When there is 100 Watt solarpower production that can be consumed.... then I want to put an electrical heater from 2000 Watt continuous 100ms ON and 1900ms OFF.

@dceejay maybe I can use PID, when it is switching fast (and no shorter times than 10ms)

In that case have a look at node-red-contrib-timeprop . It is often used with the pid node for heater switching so should be perfect for your problem.

@Colin Great! This node is installed, looks very interesting. It needs a 0 to 1 input for a 0% tot 100% output power. But it is all new for me....

I have the electrical power production: ( p1monitor/smartmeter/production_kw : msg.payload : string[5] "1.378") 1378 Watt.
When the measured "production_kw" is more then 100 Watt. Then I want the output 5% or 0,05 higher.
And this every 10 seconds.
The output of this, between 0 and 1, goes to the node-red-contrib-timeprop.

Do you think there is a PID I can use to solve this?

I don't know yet, I don't fully understand the problem. Is the production figure affected by the heater load? So that I can better understand can you describe what this is attempting to achieve?

Yes, the production is affected by the heater.
When there is 1000 Watt Solar Production the leaves the house to the street, = injected in the public grid, then we loose those 1000 Watt.
It is better to consume the 1000 Watt, then there is 0 Watt Solar production that goes to the public grid.

So if, say the production was measured at, say 500W, why don't you increase the heater power by 500W for the next ten seconds Or possible a bit less if you want to stay on the right side of zero, so maybe increase the power by current production - 100.

That would be great.

Maybe it is possible to write this in javascript.... but not for me.

Something like this maybe

[{"id":"6c924292.4f7b54","type":"function","z":"37f5b818.697af8","name":"Calculate new output","func":"// Expects current net production in msg.payload\nconst headroom = 100    // Watts headroom to keep \nconst approachFactor = 0.8  // what fraction to go each time\nconst max = 2000        // max heater output\n// Retrieve previous saved heater output, default to 0\nlet previousOutput = context.get(\"previousOutput\") || 0\n// calculate new heater power required\nlet newOutput = previousOutput + (msg.payload - headroom) * approachFactor\n// clamp to range 0 to max\nif (newOutput > 2000) newOutput = 2000\nelse if (newOutput < 0) newOutput = 0\n// save new output\ncontext.set(\"previousOutput\", newOutput)\nmsg.payload = newOutput\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":320,"y":1060,"wires":[["4c5f3ff6.078818","23cc3b86.d2bb6c"]]},{"id":"8b9d6e8a.a7152","type":"inject","z":"37f5b818.697af8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"500","payloadType":"num","x":90,"y":1040,"wires":[["6c924292.4f7b54"]]},{"id":"b5d8ef98.66d6c8","type":"inject","z":"37f5b818.697af8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"100","payloadType":"num","x":90,"y":1080,"wires":[["6c924292.4f7b54"]]},{"id":"347534ff.4318c4","type":"inject","z":"37f5b818.697af8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"-100","payloadType":"num","x":90,"y":1120,"wires":[["6c924292.4f7b54"]]},{"id":"4c5f3ff6.078818","type":"debug","z":"37f5b818.697af8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":430,"y":1140,"wires":[]},{"id":"33191a72.6bfe7e","type":"comment","z":"37f5b818.697af8","name":"Net production","info":"","x":110,"y":1000,"wires":[]},{"id":"23cc3b86.d2bb6c","type":"range","z":"37f5b818.697af8","minin":"0","maxin":"2000","minout":"0","maxout":"1","action":"scale","round":false,"property":"payload","name":"Scale 0 to 1","x":550,"y":1060,"wires":[[]]},{"id":"aa06f32e.f5656","type":"comment","z":"37f5b818.697af8","name":"To timeprop","info":"","x":650,"y":1020,"wires":[]}]

I have included an approach factor so that it does not attempt to straight there in one go but homes in on it, as otherwise I suspect you might find it bouncing up and down too much. Try it and see. When you get to testing it use a Chart node set to show the production and the heater power, that is the best way of checking how it is working. Or perhaps you are already saving the production to Influx or something, in which case that is great. Add the heater output into the chart.

@Colin This looks very useful.
And I'm a beginner, even copying this is difficult.
I'm not at home now, is there a way to reach my hardware when I know my IP-adresses?

@Colin Can you program it on my computer? (With teamviewer, when I'm at home)
Of course I want to pay for this service. How do we arrange this?
Tomorrow I wil be at home.
Then I will be launched, I will be able to make changes myself.

No, sorry, I am retired and I don't work for money any more, I am glad to say.

I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

Having done that read the node-red docs page Working with Messages which is particularly useful.
Having done that you should then have a better understanding of what is required and can ask again with specific questions about what you still are not sure how to do.

@Colin Anyway, thanks for your help!

The forum limited me 3 hours because I'm new on the forum and I sended to much messages.
(I'm happy for you, doing what you want and not doing it for money. I have hobbies that I don't want to do for money too. It's the only way to be totally free.)