<commit_before>process.title = 'temp2mqtt';
var config = require('./etc/config.json');
var w1temp = require('w1temp');
var mqtt = require('mqtt');

w1temp.getSensor(config.deviceId).then( sensor => {
  var client = mqtt.connect(config.mqtt.url);
  client.on('connect', connack=>{
    console.log("Successfully connected to MQTT");
  });
  var lastTemp = false;
  console.log("Listening for temperature changes");
  sensor.on('change', temp => {
    temp = Math.round(temp*10)/10;
    if( lastTemp !== temp ) {
      lastTemp = temp;
      client.publish(config.mqtt.topic,temp.toString(), err => {
        if( err )
          console.error('Failed to publish temperature reading to MQTT: '+err);
      });
    }
  });
}, err => {
  console.error('Failed to get sensor: '+err);
});<commit_msg>use average<commit_after>process.title = 'temp2mqtt';
var config = require('./etc/config.json');
var w1temp = require('w1temp');
var mqtt = require('mqtt');

var client = mqtt.connect(config.mqtt.url);
client.on('connect', connack=>{
  console.log("Successfully connected to MQTT");
});

function publish(temp) {
  client.publish(config.mqtt.topic,temp.toString(), err => {
  if( err )
    console.error('Failed to publish temperature reading to MQTT: '+err);
  });
}

w1temp.getSensor(config.deviceId).then( sensor => {
  var lastTemp = false;
  var log = [];
  var action = () => {
    var temp = sensor.getTemperature();
    log.unshift(temp);
    if( log.length > 30 ) log.pop();
    var av = 0;
    log.map( v => { av += v } );
    av = Math.round((av / log.length)*10)/10;
    if( lastTemp !== av ) {
      lastTemp = av;
      publish(av);
    }
  };
  
  setTimeout( action, 1000);

}, err => {
  console.error('Failed to get sensor: '+err);
});

{'fraction_non_alphanumeric': 0.1815578465063001, 'fraction_numerical': 0.013172966781214204, 'mean_word_length': 3.4116161616161618, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4695740', 'n_tokens_mistral': 605, 'n_tokens_neox': 592, 'n_words': 133}