This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

MqTT in Wiser for KNX
#20
You can create a scheduled script that runs every minute and publishes all values. Each message has to specified manually due to a variable format. Add as many addmessage calls as needed and change grp.getvalue addresses. The script will automatically disconnect once all messages are published.

Code:
broker = 'mqtt.eniris.be'

username = 'xxx'
password = 'xxx'

port = 1883

messages = {}
time = os.time()

function addmessage(topic, data)
  messages[ #messages + 1 ] = {
    topic = topic,
    data = require('json').encode(data),
  }
end

addmessage('standard1/rp_one_m/waterMetrics/' .. username, {
  time = time,
  extraTags = {
    subId = 'T19',
  },
  fields = {
    absVolume_m3 = grp.getvalue('1/1/1'),
    flow_m3ph = grp.getvalue('1/1/2'),
  }
})

addmessage('standard1/rp_one_m/weatherMetrics/' .. username, {
  time = time,
  extraTags = {
    subId = 'T19',
  },
  fields = {
    outdoorTemperature_degC = grp.getvalue('1/1/3'),
    outdoorBrightness_lux = grp.getvalue('1/1/4'),
    windSpeed_kmph = grp.getvalue('1/1/5'),
  }
})

addmessage('standard1/rp_one_m/gasMetrics/' .. username, {
  time = time,
  extraTags = {
    subId = 'T19',
  },
  fields = {
    absVolume_Nm3 = grp.getvalue('1/1/6'),
  }
})

mqtt = require('mosquitto')
client = mqtt.new()

mids = {}
pubcount = 0

client.ON_CONNECT = function(status, rc, msg)
  if status then
    log('mqtt cloud connected')

    for _, message in ipairs(messages) do
      local mid = client:publish(message.topic, message.data)

      if mid then
        pubcount = pubcount + 1
        mids[ mid ] = true
      end
    end

    if pubcount == 0 then
      client:disconnect()
    end
  else
    log('mqtt cloud connect failed ' .. tostring(msg))
    client:disconnect()
  end
end

client.ON_PUBLISH = function(mid)
  if mids[ mid ] then
    mids[ mid ] = nil
    pubcount = pubcount - 1

    if pubcount == 0 then
      log('disconnecting')
      client:disconnect()
    end
  end
end

client:login_set(username, password)
status, rc, msg = client:connect(broker, port)

if status then
  client:loop_forever()
else
  log('cloud connect failed: ' .. tostring(msg))
end
Reply


Messages In This Thread
MqTT in Wiser for KNX - by JRP - 20.05.2020, 12:11
RE: MqTT in Wiser for KNX - by Daniel - 20.05.2020, 12:40
RE: MqTT in Wiser for KNX - by JRP - 20.05.2020, 13:55
RE: MqTT in Wiser for KNX - by Daniel - 20.05.2020, 13:59
RE: MqTT in Wiser for KNX - by JRP - 20.05.2020, 18:34
RE: MqTT in Wiser for KNX - by Daniel - 21.05.2020, 07:21
RE: MqTT in Wiser for KNX - by JRP - 21.05.2020, 10:40
RE: MqTT in Wiser for KNX - by Daniel - 21.05.2020, 10:42
RE: MqTT in Wiser for KNX - by JRP - 21.05.2020, 11:20
RE: MqTT in Wiser for KNX - by Daniel - 21.05.2020, 11:22
RE: MqTT in Wiser for KNX - by JRP - 21.05.2020, 11:47
RE: MqTT in Wiser for KNX - by Daniel - 21.05.2020, 11:50
RE: MqTT in Wiser for KNX - by JRP - 21.05.2020, 15:28
RE: MqTT in Wiser for KNX - by admin - 26.08.2022, 10:38
RE: MqTT in Wiser for KNX - by admin - 26.08.2022, 11:06
RE: MqTT in Wiser for KNX - by admin - 26.08.2022, 12:55
RE: MqTT in Wiser for KNX - by JRP - 19.05.2023, 10:18
RE: MqTT in Wiser for KNX - by admin - 19.05.2023, 10:24
RE: MqTT in Wiser for KNX - by JRP - 19.05.2023, 10:42
RE: MqTT in Wiser for KNX - by JRP - 20.05.2023, 17:10
RE: MqTT in Wiser for KNX - by admin - 22.05.2023, 06:52
RE: MqTT in Wiser for KNX - by JRP - 26.05.2023, 08:21

Forum Jump: