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.

Shelly - WiFi IoT-Devices
#22
(20.06.2022, 13:14)a455115 Wrote:
Code:
if not broker then
  broker = '127.0.0.1'

  function multiply(mult)
    return function(value)
      local num = tonumber(value)
      if num then
        return num * mult
      else
        return value
      end
    end
  end

  -- topic to object map
  mqtt_to_object = {
    ['shellies/shellyplug-s-D9AA39/relay/0'] = '32/1/1',
    ['shellies/shellyplug-s-D9AA39/temperature'] = '32/1/2',
  }

-- optional topic value conversion function
mqtt_to_object_conv = {
  ['shellies/shellyplug-s-D9AA39/relay/0'] = multiply(1),
   ['shellies/shellyplug-s-D9AA39/temperature'] = multiply(1),
}

  -- object to topic map
object_to_mqtt = {
   -- ['1/1/1'] = 'out/topic1',
   -- ['1/1/2'] = 'out/topic2',
  }

  datatypes = {}

  grp.sender = 'mq'
  require('socket')

  for addr, _ in pairs(object_to_mqtt) do
    local obj = grp.find(addr)
    if obj then
      datatypes[ addr ] = obj.datatype
    end
  end

  mclient = require('mosquitto').new()

  mclient.ON_CONNECT = function(res, ...)
    log('mqtt connect status', res, ...)

    if res then
      for topic, _ in pairs(mqtt_to_object) do
        mclient:subscribe(topic)
      end
    else
      mclient:disconnect()
    end
  end

  mclient.ON_MESSAGE = function(mid, topic, payload)
    local addr = mqtt_to_object[ topic ]
    if addr then
      local fn = mqtt_to_object_conv[ topic ]

      if fn then
        payload = fn(payload)
      end

      grp.write(addr, payload)
    end
  end

  mclient.ON_DISCONNECT = function(...)
    log('mqtt disconnect', ...)
    mclientfd = nil
  end

  function mconnect()
    local fd

    mclient:connect(broker)
    fd = mclient:socket()

    -- fd ref is valid
    if fd then
      mclientfd = fd
    end
  end

  mconnect()

  function publishvalue(event)
    -- message from us or client is not connected
    if event.sender == 'mq' or not mclientfd then
      return
    end

    local addr = event.dst
    local dpt = datatypes[ addr ]
    local topic = object_to_mqtt[ addr ]

    -- unknown object
    if not dpt or not topic then
      return
    end

    local value = busdatatype.decode(event.datahex, dpt)
    if value ~= nil then
      if type(value) == 'boolean' then
        value = value and 1 or 0
      end

      mclient:publish(topic, tostring(value))
    end
  end

  lbclient = require('localbus').new(1)
  lbclient:sethandler('groupwrite', publishvalue)

  lbclientfd = socket.fdmaskset(lbclient:getfd(), 'r')

  -- run timer every 5 seconds
  timer = require('timerfd').new(5)
  timerfd = socket.fdmaskset(timer:getfd(), 'r')
end

-- mqtt connected
if mclientfd then
  mclientfdset = socket.fdmaskset(mclientfd, mclient:want_write() and 'rw' or 'r')
  res, lbclientstat, timerstat, mclientstat =
      socket.selectfds(10, lbclientfd, timerfd, mclientfdset)
-- mqtt not connected
else
  res, lbclientstat, timerstat =
    socket.selectfds(10, lbclientfd, timerfd)
end

if mclientfd and mclientstat then
  if socket.fdmaskread(mclientstat) then
    mclient:loop_read()
  end

  if socket.fdmaskwrite(mclientstat) then
    mclient:loop_write()
  end
end

if lbclientstat then
  lbclient:step()
end

if timerstat then
  -- clear armed timer
  timer:read()

  if mclientfd then
    mclient:loop_misc()
  else
    mconnect()
  end
end
Hi admin,
How do I convert directly from the code to get a specific parameter from the Json it returns? For an example of a specific topic it returns json to me 

shellies/shellytrv-14B457E5C2C8/info
return
{"tmp": {"value":24.5,"units": "C","is_valid": true},"target_t":{"enabled":false,"value":22.0,"units":"C "},"temperature_offset":0.0,"bat":99}

I only want to extract from it:
target_t - value - 1/1/1
bat - 1/1/2
How can I extract only this with the above code?
Reply


Messages In This Thread
Shelly - WiFi IoT-Devices - by Todayslife - 24.11.2020, 13:27
RE: Shelly - WiFi IoT-Devices - by admin - 24.11.2020, 13:35
RE: Shelly - WiFi IoT-Devices - by Todayslife - 28.11.2020, 14:31
RE: Shelly - WiFi IoT-Devices - by admin - 28.11.2020, 14:55
RE: Shelly - WiFi IoT-Devices - by Todayslife - 28.11.2020, 15:41
RE: Shelly - WiFi IoT-Devices - by admin - 30.11.2020, 07:21
RE: Shelly - WiFi IoT-Devices - by admin - 26.12.2020, 16:28
RE: Shelly - WiFi IoT-Devices - by admin - 28.12.2020, 07:49
RE: Shelly - WiFi IoT-Devices - by a455115 - 20.06.2022, 13:14
RE: Shelly - WiFi IoT-Devices - by a455115 - 13.10.2022, 14:26
RE: Shelly - WiFi IoT-Devices - by admin - 22.06.2022, 09:27
RE: Shelly - WiFi IoT-Devices - by a455115 - 24.06.2022, 10:21
RE: Shelly - WiFi IoT-Devices - by admin - 27.06.2022, 12:24
RE: Shelly - WiFi IoT-Devices - by a455115 - 29.06.2022, 13:45
RE: Shelly - WiFi IoT-Devices - by a455115 - 30.06.2022, 15:12
RE: Shelly - WiFi IoT-Devices - by admin - 30.06.2022, 15:19
RE: Shelly - WiFi IoT-Devices - by a455115 - 05.07.2022, 06:17
RE: Shelly - WiFi IoT-Devices - by admin - 01.07.2022, 06:09
RE: Shelly - WiFi IoT-Devices - by admin - 05.07.2022, 06:36
RE: Shelly - WiFi IoT-Devices - by a455115 - 05.07.2022, 10:40
RE: Shelly - WiFi IoT-Devices - by admin - 14.10.2022, 06:17
RE: Shelly - WiFi IoT-Devices - by a455115 - 17.10.2022, 09:53
RE: Shelly - WiFi IoT-Devices - by admin - 17.10.2022, 10:00
RE: Shelly - WiFi IoT-Devices - by a455115 - 17.10.2022, 14:51
RE: Shelly - WiFi IoT-Devices - by a455115 - 20.10.2022, 06:48
RE: Shelly - WiFi IoT-Devices - by admin - 20.10.2022, 07:44
RE: Shelly - WiFi IoT-Devices - by tomnord - 03.01.2024, 10:12
RE: Shelly - WiFi IoT-Devices - by admin - 03.01.2024, 10:19
RE: Shelly - WiFi IoT-Devices - by tomnord - 03.01.2024, 10:26
RE: Shelly - WiFi IoT-Devices - by tomnord - 03.01.2024, 14:46
RE: Shelly - WiFi IoT-Devices - by admin - 03.01.2024, 15:00
RE: Shelly - WiFi IoT-Devices - by tomnord - 16.01.2024, 08:51
RE: Shelly - WiFi IoT-Devices - by admin - 16.01.2024, 09:03
RE: Shelly - WiFi IoT-Devices - by tomnord - 16.01.2024, 09:49
RE: Shelly - WiFi IoT-Devices - by admin - 16.01.2024, 09:50
RE: Shelly - WiFi IoT-Devices - by tomnord - 16.01.2024, 09:54

Forum Jump: