Code Monkey home page Code Monkey logo

Comments (5)

curzon01 avatar curzon01 commented on August 15, 2024

possible:

as historical data creation is not functional part of the python daemon, I suggest to handle this on SQL side too.

Remove existing trigger mqtt_after_update with the following mqtt_before_update trigger:

DROP TRIGGER IF EXISTS `mqtt_after_update`;

DROP TRIGGER IF EXISTS `mqtt_before_update`;
DELIMITER //
CREATE TRIGGER `mqtt_before_update` BEFORE UPDATE ON `mqtt` FOR EACH ROW BEGIN
    IF ((SELECT value FROM mqtt WHERE id=NEW.id)!=NEW.value) THEN
        INSERT INTO mqtt_history SET ts=NEW.ts, topicid=NEW.id, value=NEW.value;
    END IF;
END//
DELIMITER ;

This will insert data into history table only if the current value is different to the last published

from mqtt2sql.

curzon01 avatar curzon01 commented on August 15, 2024

An alternate way with detailed control for each topic is

  1. add an additonal field history
ALTER TABLE `mqtt`
	ADD COLUMN `history` TINYINT(4) NOT NULL DEFAULT '1' AFTER `active`;
  1. insert values (check in triggers) only if history is set, so change the existing original triggers like:
DROP TRIGGER IF EXISTS `mqtt_after_insert`;
DROP TRIGGER IF EXISTS `mqtt_after_update`;
DELIMITER //
CREATE TRIGGER `mqtt_after_insert` AFTER INSERT ON `mqtt` FOR EACH ROW BEGIN
	IF NEW.history!=0 THEN
		INSERT INTO mqtt_history SET ts=NEW.ts, topicid=NEW.id, value=NEW.value;
	END IF;
END//
CREATE TRIGGER `mqtt_after_update` AFTER UPDATE ON `mqtt` FOR EACH ROW BEGIN
	IF NEW.history!=0 THEN
		INSERT INTO mqtt_history SET ts=NEW.ts, topicid=NEW.id, value=NEW.value;
	END IF;
END//
DELIMITER ;

This solution allows you to fine control using the history column of mqtt table which topic should be in history and which not.

from mqtt2sql.

gsantner avatar gsantner commented on August 15, 2024

Cool thanks for the statements! I think they would also fit good for README?

I prefer the history for all, and no exclusions, so nr 1 😃 .

EDIT: Actually, I think it would make sense to add the "history" flag in general (i.e. to the default README statement), and have the trigger "changed only" optionally as "alternative trigger setup"?

from mqtt2sql.

gsantner avatar gsantner commented on August 15, 2024

I've created a PR to add the history flag to mqtt mysql table on README.

If you are interested, after that PR we could upgrade it to have both suggestions coexisting in DB and in trigger: enable_history and enable_history_changes_only

from mqtt2sql.

gsantner avatar gsantner commented on August 15, 2024

btw, what I want todo with the data that this script produces: create simple dashboard for e.g. temperature history and turn heating on/off.
I'm running openhab since quite some time but it's quite unstable and resource consuming while I only need a very small faction of features. ... as data anyway coming from mqtt this should be enough to realize it in the simplest fashion

from mqtt2sql.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.