Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timestamp for mqtt messages #37

Open
cmaxmedia opened this issue Dec 12, 2018 · 1 comment
Open

Timestamp for mqtt messages #37

cmaxmedia opened this issue Dec 12, 2018 · 1 comment

Comments

@cmaxmedia
Copy link

Hi everyone, for a retained mqtt topic i can use %deviceName% or %roomName% as dynamic variables - is there a %timestamp% tag or any other way to publish the timestamp of the last on/off toggle?
cheers

@cmaxmedia
Copy link
Author

I have implemented the following in /opt/z-way-server/automation/userModules/MQTT/index.js. Now the mqtt publishes a JSON with a timestamp and the value instead of string "on" or "off" only

//function added for leading zero
MQTT.prototype.trimString = function( txt, len ) {
  if( txt.toString().length==1 ) return "0" + txt.toString();
  return txt;
};

//updated function publish
MQTT.prototype.publish = function (topic, value, retained) {
  var self = this;

  if( self.client && self.client.connected) {
    var options = {};
    options.retain = retained;

    var dt = new Date();
    var dtString = dt.getFullYear() + "/" + self.trimString( dt.getMonth()+1, 2 ) + "/" + self.trimString( dt.getDate(), 2 );
    dtString += " " + self.trimString( dt.getHours(), 2 ) + ":" + self.trimString( dt.getMinutes(), 2 ) + ":" + self.trimString( dt.getSeconds(), 2 );

    var value_publish = '{"ts":"' + dtString + '","value":"' + value + '"}';

    self.client.publish( topic, value_publish.toString().trim(), options );
  }
};

cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant