 args = clone(root.arguments)  # don't need to do this, just for fun
 set = parse_opts({test: test},args)
 appliances = read_json(state_db_fn)
-print "# in:",set.event,set.id
-appl = new HeyuAppliance(set.id, states: ['OFF', 'ON'])
-appl.changeState('any',set.event)
-appliances.push appl
+if set.id
+  print "# in:",set.event,set.id
+  appl = new HeyuAppliance(set.id, states: ['OFF', 'ON'])
+  appl.changeState('any',set.event)
+  appliances[appl.name] = appl
 print "# Saving state to",state_db_fn
-write_json(state_db_fn,appliances)
+list = []
+for name,appl2 of appliances
+  appl2.display_state()
+  list.push appl2
+write_json(state_db_fn,list)

    appliances[name] = appl
  appliances

# ---- Write JSON
write_json = (fn,objs) ->
  # Try to write to a file
  file = new File(fn)


# ---- Read JSON file
read_json = (fn) ->
  file = new File(fn)
  return {} if not file.exists
  load(fn) # Use the JS parser
  for k,v of state_machines
    print '#',k,v
