Using ChatGPT to write Node-Red Code

After watching the webinar replay on the AI assistant in Flowfuse I decided to give it a try using chatGPT.

Note: Flowfuse also use ChatGPT in their AI assistant.

One of the examples in the video was to generate sample test data so I thought I would give it a try.



This is the query I used

chatgpt-query

The code is below. There is a also a description of  how it works generated by ChatGPT and I have included it below the code.

// Initialize the context variable to track the previous value
if (typeof context.previousValue === 'undefined') {
context.previousValue = 20; // Initial value centered around 20
}

// Function to generate a random number within a range
function randomInRange(min, max) {
return Math.random() * (max - min) + min;
}

// Generate a small or large deviation
let deviation;
if (Math.random() < 0.1) {
// 10% chance of a large deviation (±10%)
deviation = randomInRange(-0.1, 0.1);
} else {
// 90% chance of a small deviation (±5%)
deviation = randomInRange(-0.05, 0.05);
}

// Calculate the new value
let newValue = context.previousValue * (1 + deviation);

// Ensure the value stays between 0 and 100
newValue = Math.max(0, Math.min(100, newValue));

// Update the previous value in the context
context.previousValue = newValue;

// Create the output message
msg.payload = newValue;
msg.topic = "data";

// Return the message to be sent
return msg;

How it Works:

  • Initial Setup: The function starts by checking if the previousValue is already defined in the context. If not, it initializes it to 20.
  • Deviation Calculation:
    • The function then decides whether to apply a small (±5%) or a large (±10%) deviation. This is controlled by a random chance, where there’s a 10% chance for a large deviation and a 90% chance for a small deviation.
    • The deviation is applied to the previous value.
  • Value Constraint: The result is then clamped between 0 and 100 to ensure it stays within this range.
  • Message Creation: Finally, the function sets the value as msg.payload, assigns the topic “data” to msg.topic, and returns the message.

Usage:

  1. Add a Function node to your Node-RED flow.
  2. Paste the code above into the Function node.
  3. Add an Inject node to trigger this function every second (set the Inject node to repeat every 1 second).
  4. Connect the Inject node to the Function node.
  5. Add a Debug node to view the output or route the output to another node in your flow.

This setup will send a new message every second with the described properties.

Demo Flow

I created a demo flow as shown below using the function node without any edits:

AI-demo-flow
The result is show in the graph below.It starts well but slowly drifts down as you can see below.

chatgpt-flow-data

So the function node does need some manual editing but overall a good result .

Using ChatGpt should make writing function node code much easier and quicker.

Note: You will need a ChatGpt account  to repeat this yourself, but they are free.

Related tutorials and resources:

 

Click to rate this post!
[Total: 1 Average: 5]

Leave a Reply

Your email address will not be published. Required fields are marked *