Hi!
Could anyone point me in the right direction to writing a query that turns state changes (On/off) into an average of % on over time. This is definitely the deep end for me, so if I am missing something big, or this is a dumb question please let me know where I lack understanding.
Background: I’m an intern developing a front end display for CNC machine metrics, using hassio and grafana. We store metrics on a postgreSQL server within the business. Hassio outputs state “on” and “off”. Below is the basic schema of the Database I am using.
CREATE TABLE events (
event_id INTEGER NOT NULL,
event_type VARCHAR(32),
event_data TEXT,
origin VARCHAR(32),
time_fired DATETIME,
created DATETIME,
PRIMARY KEY (event_id)
)
CREATE INDEX ix_events_event_type ON events (event_type)
CREATE TABLE recorder_runs (
run_id INTEGER NOT NULL,
start DATETIME,
“end” DATETIME,
closed_incorrect BOOLEAN,
created DATETIME,
PRIMARY KEY (run_id),
CHECK (closed_incorrect IN (0, 1))
)
CREATE TABLE states (
state_id INTEGER NOT NULL,
domain VARCHAR(64),
entity_id VARCHAR(64),
state VARCHAR(255),
attributes TEXT,
event_id INTEGER,
last_changed DATETIME,
last_updated DATETIME,
created DATETIME,
PRIMARY KEY (state_id),
FOREIGN KEY(event_id) REFERENCES events (event_id)
)