Hi,
I have just discovered node red and it seems fantastic. I am trying to put together a smart home system and it seems really quick to do development. At the moment I am just in the process of setting up various sensors (dht22, bme280, ds18b20, etc) around the house (the smart bit will come later). I want to store the values to a db (I have been just using python up until now to insert into sqlite tables for each sensor). I am now ready to make the database a bit more coherent and I was wondering if anyone could give me some feedback on the design before I put it into action (I know it is not exactly a node red question but I think that lots of people here are probably doing similar stuff). Anyway here is the structure that I am proposing. It is basically a relational database to store all values in one table with references to regions and sensors in the others. I am pretty new to db design so would appreciated any feedback before going too much further.
Thanks in advance for reading,
Martyn
DB design:
-- Table: tbl_measurement
CREATE TABLE tbl_measurement (id INTEGER PRIMARY KEY AUTOINCREMENT, measurement VARCHAR (15) NOT NULL);
-- Table: tbl_region
CREATE TABLE tbl_region (id INTEGER PRIMARY KEY AUTOINCREMENT, region VARCHAR (20) NOT NULL);
-- Table: tbl_room
CREATE TABLE tbl_room (id INTEGER PRIMARY KEY AUTOINCREMENT, region_id INTEGER CONSTRAINT fk_region REFERENCES tbl_region (id) NOT NULL, room VARCHAR (20) NOT NULL);
-- Table: tbl_room_sensor_measurement
CREATE TABLE tbl_room_sensor_measurement (id INTEGER PRIMARY KEY AUTOINCREMENT, room_id INTEGER CONSTRAINT fk_room REFERENCES tbl_room (id) NOT NULL, sensor_measurement_id INTEGER NOT NULL CONSTRAINT fk_sensor_measurement REFERENCES tbl_sensor_measurement (id), instance INTEGER NOT NULL, CONSTRAINT un_room_sensor_instance UNIQUE (room_id, sensor_measurement_id, instance) ON CONFLICT ROLLBACK);
-- Table: tbl_sensor
CREATE TABLE tbl_sensor (id INTEGER PRIMARY KEY AUTOINCREMENT, sensor VARCHAR (15) NOT NULL);
-- Table: tbl_sensor_measurement
CREATE TABLE tbl_sensor_measurement (id PRIMARY KEY, sensor_id INTEGER CONSTRAINT fk_sensor REFERENCES tbl_sensor (id) NOT NULL, measurement_id INTEGER CONSTRAINT fk_measurement REFERENCES tbl_measurement (id) NOT NULL, CONSTRAINT un_sensor_measurement UNIQUE (sensor_id, measurement_id) ON CONFLICT ROLLBACK);
-- Table: tbl_value
CREATE TABLE tbl_value (id INTEGER PRIMARY KEY AUTOINCREMENT, room_sensor_measurement_id INTEGER CONSTRAINT fk_room_sensor_measurement REFERENCES tbl_room_sensor_measurement (id) NOT NULL, value NUMERIC NOT NULL, timestamp INTEGER (4) NOT NULL);