Personalize the MQTT topics



  • Hi there all.
    I finally finish to make work my first node and I'm very happy that it's work greate.

    I just want to ask if it's possible to personalize the MQTT topics.
    I mean, if it's possible in the GATEWAY code to intercept the NodeID, SensorID and value
    and make a personalized topics in order to send it to MQTT broker.

    I ask this because I make just a unique node and in my MQTT
    it's already a lot of confusion...
    There is a lot of number that I must translate every time I take a look.

    What I would like is... use the MQTT code in order to make the broker connection,
    stop the default update of topics, intercept the NodeID/SensorID/Value and give e personalized name.

    Thanks all in advance
    Denis


  • Mod

    @DenisJ that should be possible, although mysensors does not implement it.

    What you could do is use the MQTT MySensors gateway as-is, and use a service on top, running somewhere else, that subscribes to the mysensors MQTT topics, 'decodes' them and republishes the data under different, understandable topics to the MQTT broker.

    This way you are not deviating from the MySensors gateway implementation (e.g. when updates are released) and you do not have to modify & reflash your gateway each time a node is added.

    I personally use node-red for the decoding, but any other 'programming' tool with MQTT access should work.



  • Thanks a lot Yveaux... it's just what I work to from about 2 hours 🙂
    I'll split all sensors in Node-Red with a new topic name.
    I use something like:

    /*
    id SensGarag: 11
    id SensCame1: 
    id SensCame2: 
    id SensCame3: 
    id SensCucin: 
    id SensFuori: 
    id SensSalon: 
    id SensSerra: 
    type Temp : 51
    type Hum  : 52
    type Press: 53
    */
    
    // sensor id (topic:MySensors-out/xx/)
    var parts = msg.topic.split("/");
    id   = parts[1];
    type = parts[2];
    

    Thanks a lot again
    Denis


  • Mod

    @DenisJ nice!
    Btw. @tbowmo created a nice set of node-red nodes to de- and encode MySensors messages. Maybe they can be of good use to you.



  • I have try to install it but node-red give me some error.
    But I have make a function that have a tabel that personalize all topics.
    f63850b0-813f-4aa3-aff0-9f9f85ca6e8c-image.png
    So I'll make all my future sensors in this mode.

    Thanks again
    Denis



  • I'm interested, can you share some more info on this feature ??



  • Practically I receive the MQTT topics from the MySensors Gateway.
    Then in Node-Red I have make a function that translate the MySensors MQTT topics in a personalized MQTT topics,
    in order to have clear names of the sensors value.

    In Node-Red you make a flow like this:
    d6af473c-7ae1-4977-834e-04fcf8f98b26-image.png

    Then in the function node you make something like this... but personalized:

    // add timestamp to every sensor reads
    var dt = new Date();
    var hh = dt.getHours();
    var mm = dt.getMinutes();
    var ss = dt.getSeconds();
    var yy = dt.getFullYear();
    var mo = dt.getMonth() + 1; // want 1 more
    var dd = dt.getDate();
    //var daynum = date.getDay();
    timeST = (dd < 10 ? "0" : "") + dd + "-" + (mo < 10 ? "0" : "") + mo + "-" + yy + 
      " " + (hh < 10 ? "0" : "") + hh + ":" + (mm < 10 ? "0" : "") + mm + ":" + (ss < 10 ? "0" : "") + ss;
    
    /* sensors ID
    id SensGaraj: 11
    id SensCame1: 
    id SensCame2: 
    id SensCame3: 
    id SensCucin: 
    id SensSerra: 12
    id SensFuori: 
    id SensSalon: 
    
    type Temp  : 51
    type Hum   : 52
    type Press : 53
    type BatVol: 54
    type BatLev: 255 
    */
    
    ret = 0; // send the return or not
    
    // sensor id (topic:MySensors-out/xx/) 
    var parts = msg.topic.split("/");
    id   = parts[1];
    type = parts[2];
    val  = msg.payload;
    
    if (id == 11 ){sname = "SensGaraj";ret = 1;}
    if (id == 12 ){sname = "SensSerra";ret = 1;} 
    // if etc...
    
    if (ret == 1){
        if (type == 51 ){stype = "Temp"; }
        if (type == 52 ){stype = "Hum";}
        if (type == 53 ){stype = "Press";}
        if (type == 54 ){stype = "BattVol";}
        if (type == 255){stype = "BattLev";}
        msg = {}; //initialize msg object
        msg.measurement = "sensors"; // tabella/file > influxdb
        msg.topic = "MySensors/" + sname + "/" + stype;
        msg.payload = {value: val, timeST: timeST}
        node.status({fill:"green",shape:"ring",text:sname});
        return msg;
    }
    //////////////////////////////////////////////////////////////
    

    If you need more help just let me know 🙂

    Denis


Log in to reply
 

Suggested Topics

127
Online

10.2k
Users

10.6k
Topics

109.2k
Posts