import asyncdispatch, strutils, json, tables, options import ../types, ../vars, ../deviceActionHandler import nmqtt proc handleMotionSensor (topic: string, message: string) = try: let deviceName = zigbee2mqttDevices[topic] let config = server.config.devices[deviceName] let recivedData = parseJson(message) if recivedData.hasKey("occupancy"): if recivedData["occupancy"].getBool == true: for action in config.occupyActions: discard waitFor handleDeviceAction(action) if recivedData["occupancy"].getBool == false: for action in config.clearActions: discard waitFor handleDeviceAction(action) except: echo "Error[handleMotionSensor]:\n", getCurrentExceptionMsg() proc initZigbee2MqttMotionSensors* () {.async.} = for key, device in server.config.devices.pairs(): if device.type != Zigbee2MqttMotionSensor: continue zigbee2mqttDevices["zigbee2mqtt/" & device.deviceName.get] = key await mqttContext.subscribe("zigbee2mqtt/" & device.deviceName.get, 2, handleMotionSensor)