hy, i need a help in this situation
I have this topic which sends the voltage values in real time and the type of msg.playload which is sent is "number", I do this function to transform the type to array to insert into the sqlite table but the result is deferred in debug 1
Not sure what sqlite node you are using but the node-red-node-sqlite (node) - Node-RED seems to suggest the values should be ($tension).
Hi, this works for JSON in SQlite and as been working for over a year, use any parts that are useful to you !
I got the help a year ago, so just passing on what worked for me
// ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
let device = msg.device; // The last part of the IP address.
let table = msg.table; // The database table name.
let new_date = new Date(); // Does NOT change with time zones across the world.
let iso = new Date().toISOString(); // RFC 3339 format ... for Grafana etc.
let timestamp = Math.round(new_date.getTime() / 1000); // getTime = milliseconds ... this removes last 3 digits to change to seconds ... for Grafana etc.
// ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
let data =
{
payload: msg.data,
payload_01: msg.payload_01,
payload_02: msg.payload_02, // Anything that is not below listed below.
temperature: msg.temperature, // The temperature value.
humidity: msg.humidity, // The humidity value.
sensor: msg.sensor, // Used for a sensor value that is not covered by others.
battery: msg.battery, // The battery voltage.
info: msg.info,
cost: msg.cost,
location: msg.location, // Where is it located in my home ?
id: msg.id, // Description for database, that identifies this data from others that use the same shortened IP address.
iso: iso, // Used by Grafana etc.
timestamp: timestamp, // Used by Grafana etc.
};
// ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
data = JSON.stringify(data); // Converts a JavaScript value to a JSON string
const Data_sequence = `( device,device_data) values('${device}','${data}')` // This is the actual data.
const topic = `INSERT INTO ${table} ${Data_sequence}` // The insert string to send to database.
msg.payload = "";
msg.topic = topic;
return msg;
As E1cid mentioned the question mark is not sufficient anymore, in the past this was fine but is changed. You need to add the $ to your variable.
For example.
msg2.topic = "INSERT INTO LichtSchak (timestamp, output2) VALUES ($timestamp, $output2)";
msg2.payload = [new Date(), msg.payload];