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 broker app communication with local client event based script
#1
broker = "localhost"
port = 1883
username = "admin"
password = "password"
topic = "casambi/1/set/target_level"

value = event.getvalue()

if value == 1
  then
  topic = "casambi/1/set/target_level"
payload = tostring"("level":255,"duration":1,"targetid":177,"targettype":1,)"
else
  payload = tostring"("level":0,"duration":1,"targetid":177,"targettype":1,)"
  end
 
mqtt = require("mosquitto")
client = mqtt.new()

client.ON_CONNECT = function(status, rc, msg)
  if status then
    log("mqtt connected")
    client:publish(topic, tostring(payload))
  else
    log("mqtt connect failed " .. tostring(msg))
    client:disconnect()
  end
end

client.ON_PUBLISH = function()
  client:disconnect()
end

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

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

Error log shows:
attempt to call global 'level' (a nil value)
stack traceback:
Reply
#2
Your tostring() call is incorrect. If this is an event script then value is true / false not 1 / 0. Are you sure about the payload format? Is it JSON?
Reply
#3
(13.02.2023, 12:31)admin Wrote: Your tostring() call is incorrect. If this is an event script then value is true / false not 1 / 0. Are you sure about the payload format? Is it JSON?

Yes the paylod format is in JSON format
Reply
#4
Code:
value = event.getvalue()

level = value and 255 or 0

payload = require('json').encode({
  level = level,
  duration = 1,
  targetid = 177,
  targettype = 1,
})

mqtt = require("mosquitto")
Reply
#5
Thank you admin it's working. but for to get the data from mqtt server in json and load the particular data in group address on event based script.
Reply
#6
See this example: https://forum.logicmachine.net/showthrea...6#pid10926
Reply


Forum Jump: